1

What I have is an ArrayList of objects and I am trying to use reflection to get the name of each property of each object in the ArrayList. For example:

private class TestClass
{
    private int m_IntProp;

    private string m_StrProp;
    public string StrProp
    {
        get
        {
            return m_StrProp;
        }

        set
        {
            m_StrProp = value;
        }
    }

    public int IntProp
    {
        get
        {
            return m_IntProp;
        }

        set
        {
            m_IntProp = value;
        }
    }
}

ArrayList al = new ArrayList();
TestClass tc1 = new TestClass();
TestClass tc2 = new TestClass();
tc1.IntProp = 5;
tc1.StrProp = "Test 1";
tc2.IntProp = 10;
tc2.StrPRop = "Test 2";
al.Add(tc1);
al.Add(tc2);

foreach (object obj in al)
{
    // Here is where I need help
    // I need to be able to read the properties
    // StrProp and IntProp. Keep in mind that
    // this ArrayList may not always contain
    // TestClass objects. It can be any object,
    // which is why I think I need to use reflection.
}
5
  • Any reason you're using an ArrayList and not a List<TestClass>? Commented Jun 22, 2011 at 20:23
  • @dtb - Yes, because my list may not necessarily be a TestClass. It can hold any object. Commented Jun 22, 2011 at 20:24
  • 1
    List<object> would be more appropriate. Commented Jun 22, 2011 at 20:28
  • Maybe you could also consider using an interface. But this of course would depend on the situation that you are actually using this for. Commented Jun 22, 2011 at 20:38
  • @spender - Isn't List<object> basically the same as an ArrayList? Other then maybe for clarity's sake? Commented Jun 22, 2011 at 20:39

3 Answers 3

7
foreach (object obj in al)
{
    foreach(PropertyInfo prop in obj.GetType().GetProperties(
         BindingFlags.Public | BindingFlags.Instance))
    {
        object value = prop.GetValue(obj, null);
        string name = prop.Name;
        // ^^^^ use those
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

This might look to be the best answer. I will try it now. Thanks
This works perfect. Only other thing though, Marc, is there a way to know if the property is a string, int, float, decimal, or other (none of the above)?
@icemanind well, you could check prop.PropertyType, or more conveniently a switch(Type.GetTypeCode(prop.PropertyType)) which is an enum with most of the common basic types.
1

You could use the as operator so that you don't have to use Reflection.

foreach(object obj in al)
{
     var testClass = obj as TestClass;
     if (testClass != null)
     {
        //Do stuff
     }
}

5 Comments

"Keep in mind that this ArrayList may not always contain TestClass objects. It can be any object," (from the question)
You got "as TestClass", but the List may not have TestClass objects. It may hold a different type of object all together
Casting an object other than a TestClass object would then return null would it not?
This might actually be either a great solution or not, depending on the exact intent of the questioner.
@Adam - but how would getting a null help achieve the aim of "to get the name of each property of each object in the ArrayList."
0

It's as simple as this:

PropertyInfo[] props = obj.GetType().GetProperties();

The GetType method will return the actual type, not object. Each of the PropertyInfo objects will have a Name property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.