I have a List of Lists, with each list holding a different class of object. To add elements to the correct list, I need to be able to find the type of the list. I am currently trying to accomplish it in this way:
foreach(object o in DataBase)
{
Type t = o.GetType();
if (t != typeof(T))
continue;
else
{
List<T> L = (List<T>)o;
L.Add(element);
break;
}
}
However, this returns a List Type, of System.Collections.Generic.List`1[[myType,etc,etc]].
Is there a way to extract ‘myType’ from the type of List, for a proper comparison?
Use
GetGenericArgumentsmethod:Since
myListTypeis an instance of a generic type with exactly one type parameter, the typeTwill be returned in the 0-th position of the returned array.