CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2006
    Posts
    357

    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.

  2. #2
    Join Date
    Nov 2006
    Posts
    357

    Re: Reflection And Objects

    Its ok problem solved, i can use:

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

  3. #3
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    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);
    }

  4. #4
    Join Date
    Nov 2006
    Posts
    357

    Re: Reflection And Objects

    Cheers!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured