How to find if a generic type is List<>

Go To


I have a generic method define as below

public T MyMethod<T>(extra params)

My method can receive and return either single entity type but also List. How can I find if T passed is a single entity type or a List and in this case how can I get the type of inner type T1?


2012-04-04 01:16
by bzamfir


You can do this using methods of the Type class:

var type = typeof(T);
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>)) {
    var innerType = type.GetGenericArguments()[0];
2012-04-04 01:35
by dasblinkenlight