How do I get the actual type of T in a generic List at run time using reflection?
You can use Type.GetGenericArguments to return the type T in a List
.
For example, this will return the Type for any List
passed as an argument:
Type GetListType(object list)
{
Type type = list.GetType();
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
return type.GetGenericArguments()[0];
else
throw new ArgumentException("list is not a List", "list");
}