Get C# Property Values for multiple objects -
i have couple of domain objects like:
public class person() { public int age { get; set; } public string city{ get; set; } } public class company() { public string name{ get; set; } public string address{ get; set; } }
i have class calls mymethod mentioned below.
public class calltest() { person p= new person{age=10,city="dd"}; company c= new company{name="mynae",address="myaddress"}; mymethod(p); mymethod(c); }
mi.name gives me property name. how property value?
public class mymethod(object obj) { type t = obj.gettype(); propertyinfo prop = t.getproperty("items"); foreach (memberinfo mi in t.getmembers()) { if (mi.membertype == membertypes.property) { var x = mi.name; } } }
you need cast memberinfo
propertyinfo
it's value :
..... if (mi.membertype == membertypes.property) { var x = mi.name; var value = ((propertyinfo) mi).getvalue(obj); } .....
Comments
Post a Comment