enumerate all of property and its value [duplicate]
Possible Duplicate:
Enumerating th开发者_如何学JAVArough an object's properties (string) in C#
Can I use reflection to enumerate all the property name and value of an object?
May be something like below
Say there is a object strList
PropertyInfo[] info = strList.GetType().GetProperties();
Dictionary<string, object> propNamesAndValues = new Dictionary<string, object>();
foreach (PropertyInfo pinfo in info)
{
propNamesAndValues.Add(pinfo.Name, pinfo.GetValue(strList, null));
}
try this
var properties = myObj.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public);
var propertyNames = properties.Select(p => p.Name);
var propertyValues = properties.Select(p => p.GetValue(myObj, null));
Try to use something like this:
MyClass aClass = new MyClass();
Type t = aClass.GetType();
PropertyInfo[] pi = t.GetProperties();
foreach (PropertyInfo prop in pi)
Console.WriteLine("Prop: {0}", prop.Name);
Hope it will help you.
精彩评论