Reflection of properties.

F

Frank Rizzo

Ok, I've heard all about reflection.
How do I print out the results for all the properties on this object:

System.Reflection.Assembly.GetExecutingAssembly().GetName()

I don't want a list of properties, but a list of values for the properties.
Thanks
 
J

Jon Davis

PropertyInfo objects contain a method called GetValue(...) in which you pass
the object instance.

Jon
 
F

Frank Rizzo

Jon said:
PropertyInfo objects contain a method called GetValue(...) in which you pass
the object instance.

I am not familiar with that object. Could you provide an example and/or
how to loop through all the properties?
 
J

Jon Davis

(Untested...)


using System.Reflection;

......

Hashtable PropertiesOfMyObject = new HashTable();
Type t = myObject.GetType();
PropertyInfo[] pis = t.GetProperties();
for (int i=0; i<pis.Length; i++) {
PropertyInfo pi = (PropertyInfo)pis.GetValue(i);
PropertiesOfMyObject.Add(pi.Name, pi.GetValue(myObject, new object[]
{}));
}

HTH,
Jon


Frank Rizzo said:
Jon said:
PropertyInfo objects contain a method called GetValue(...) in which you pass
the object instance.

I am not familiar with that object. Could you provide an example and/or
how to loop through all the properties?
 
F

Frank Rizzo

You are the man. I haven't tested it, but the code makes sense.

Jon said:
(Untested...)


using System.Reflection;

.....

Hashtable PropertiesOfMyObject = new HashTable();
Type t = myObject.GetType();
PropertyInfo[] pis = t.GetProperties();
for (int i=0; i<pis.Length; i++) {
PropertyInfo pi = (PropertyInfo)pis.GetValue(i);
PropertiesOfMyObject.Add(pi.Name, pi.GetValue(myObject, new object[]
{}));
}

HTH,
Jon


pass


I am not familiar with that object. Could you provide an example and/or
how to loop through all the properties?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top