How to determine type without C # type parameters to check?

Is it possible to check the type of a generic type without using any generic parameters?

For example, I would like to do something similar to the following (real type names have been changed to protect the innocent):

var list = new List<SomeType>();

...

if (list is List)
    {
    Console.WriteLine("That is a generic list!");
    }

In the above code, the following error is generated:

Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments

Is there any way around this? Preferably, something short and something that will work with types that DO NOT have common parameters (that is, "if myString is a List").

+5
source share
1 answer

You can check the following:

var type = list.GetType();
if(type.IsGenericType && 
   type.GetGenericTypeDefinition().Equals(typeof(List<>)))
{
    // do work
}
+9
source

All Articles