开发者

enumerate all of property and its value [duplicate]

This question already has answers here: Closed 12 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜