lundi 12 octobre 2015

Iterate Over a collection contained in a single object created from the class "object"

I have a single object created from the class "object". It contains a list of objects created from another class. I have drawn something similar to a sketch below.

 object temp=new object()
 {
     MyClass ObjectOne
     MyClass ObjectTwo
     MyClass ObjectThree
     ...........
 }

Now what I want is to iterate over the list contained in this single object created from the class "object".

I tried the following.

            foreach (var a in temp.GetType().GetProperties())
            {
                string test = "ParamName";
                Type t=a.GetType();
                PropertyInfo pinfo = t.GetProperty(test);


                 object[] indexArgs = {0 };
                 object value = pinfo.GetValue(temp, indexArgs);                     
            }

But it throws an exception : Object Reference Not set to an instance of object. Because "pinfo" is null. I am wondering how it is becoming null. Because I am very sure that the property "ParamName" is there in the class "MyClass"

Note : I can't access "MyClass" or cast "a" to "MyClass" because I'm using C# reflection

Please help me to find the solution.

This question is somewhat similar to Object created from the class "object" does not allow to retrieve property values.

Here, I tried to make my question clearer. Hope it is clear enough now.





Aucun commentaire:

Enregistrer un commentaire