Click to See Complete Forum and Search --> : Reflection And Objects


Grofit
October 22nd, 2008, 07:54 AM
Hey,

Just a quick question (hopefully), is there any way to loop round all the properties within an object?

Its just ive got a function with an assembly object and would like to be able to spit out all its information onto the screen... so for example:

m_Object.MyProperty1
m_Object.MyProperty2
etc...

Rather than manually making a Dictionary object with the parameters and the values and having to manually populate it i was wondering if there was any way to extract the available properties associated with that object?

Let me know if you want more clarity as it is a bit vauge, but basically i want to be able to get a list of strings that tells me all the properties an object has...

MyProperty1, Property1Value
MyProperty2, Property2Value
etc...

Grofit
October 22nd, 2008, 08:07 AM
Its ok problem solved, i can use:

foreach (PropertyDescriptor Params in TypeDescriptor.GetProperties(m_Object))
{
Console.WriteLine(Params.DisplayName);
}

MadHatter
October 22nd, 2008, 09:14 AM
MyObject myObj = new MyObject();
Type myObjType = typeof(MyObject);
foreach(PropertyInfo pi in myObjType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty)) {
string name = pi.Name;
object value = pi.GetValue(myObj, null);
}

Grofit
October 22nd, 2008, 09:52 AM
Cheers!