0

Possible Duplicate:
Enumerating through an object's properties (string) in C#

Can I use reflection to enumerate all the property name and value of an object?

Community
  • 1
  • 1
user496949
  • 79,431
  • 144
  • 301
  • 419

3 Answers3

3

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));
        }              
TalentTuner
  • 17,031
  • 5
  • 37
  • 62
1

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));
Dean Chalk
  • 19,361
  • 6
  • 58
  • 86
0

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.

BenMorel
  • 31,815
  • 47
  • 169
  • 296
Dima Shmidt
  • 895
  • 4
  • 13