How to find if a generic type is List<>

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

Answers


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];
}

Need Your Help

Listview memory leak

java android listview memory memory-leaks

I have a simple list view with adapter.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.