Xaisoft
Xaisoft

Reputation: 46591

How can I access a List<object> properties?

I have a List of objects and each object is a another List of objects, so for example, I am passing List<object> cars to one method and I want to get all the properties of one of the objects, but when I try to do:

var props = cars.First().GetType().GetProperties(BindingFlags.DeclaredOnly |
                                                         BindingFlags.Public |
                                                         BindingFlags.Instance);

It gives me the properties of System.Object (Count, Capacity, and my list of objects)

When I look at cars which in this case is called list, it looks like this:

enter image description here

Upvotes: 1

Views: 116

Answers (2)

TBD
TBD

Reputation: 781

List<List<object>> listOfCarObjects = new List<object>();

listOfCarObjects.Add(new List<object>());

LookAtCars(listOfCarObjects);

public void LookAtCars(List<objects> cars)
{
   foreach(var item in cars)
   {
         List<object> innerList = item as List<object>
         foreach(innerItem in innerList)
         {
             //do something with innerItem
         }
   }
}

Would something like that help?

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062895

As you have said: each object is another list of objects. The things you report (Count, Capacity etc) are the properties of that inner list. If you want to go deeper, cast it as IList and take a look inside the inner list.

Upvotes: 3

Related Questions