|
-
October 22nd, 2008, 07:54 AM
#1
Reflection And Objects
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...
Last edited by Grofit; October 22nd, 2008 at 08:02 AM.
-
October 22nd, 2008, 08:07 AM
#2
Re: Reflection And Objects
Its ok problem solved, i can use:
foreach (PropertyDescriptor Params in TypeDescriptor.GetProperties(m_Object))
{
Console.WriteLine(Params.DisplayName);
}
-
October 22nd, 2008, 09:14 AM
#3
Re: Reflection And Objects
Code:
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);
}
-
October 22nd, 2008, 09:52 AM
#4
Re: Reflection And Objects
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|