How to find if a generic type is List<>

Go To StackoverFlow.com

0

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?

Thanks

2012-04-04 01:16
by bzamfir


1

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
Ads