jeudi 6 juin 2019

c#: How to Access elements in a List

I'll try to make this clear:

I have an object of type System.Object that contains a List of things. The things are determined at runtime. Currently, I'm able to find out the type of things contained in the list, but I can't figure out how to access them.

Here's a code example where "o" is the List of things in question:

Example:

Type thingType = thing.GetType();
Type listType = typeof(List<>);
Type listOfThingsType = listType.MakeGenericType(thingType);

if (o.GetType() == ListOfThingsType)
{
    //Now I know o contains a list of things
    //...but how do I access them and work with their members?

    //foreach thing in o
    //   operate on thing
}





Aucun commentaire:

Enregistrer un commentaire