c# - Getting collection of all members of a class -
i want collection of members present in class. how do that? using following, giving me many names along members.
type obj = objcontactfield.gettype(); memberinfo[] objmember = obj.getmembers(); string name = objmember[5].name.tostring();
if want collection of properties , there values, :
class test { public string name { get; set; } } test instance = new test(); type type = typeof(test); dictionary<string, object> properties = new dictionary<string, object>(); foreach (propertyinfo prop in type.getproperties()) properties.add(prop.name, prop.getvalue(instance));
note, need add using system.collections.generic;
, using system.reflection;
example work.
Comments
Post a Comment