variable.GetType().IsGenericType &&
variable.GetType().GetGenericTypeDefinition() == typeof(List<>)
Of course, this only works if the variable is of type List<T>and is not a derived class. If you want to check whether this is List<T>or inherited from it, you must go through the inheritance hierarchy and check the above statement for each base class:
static bool IsList(object obj)
{
Type t = obj.GetType();
do {
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>))
return true;
t = t.BaseType;
} while (t != null);
return false;
}
source
share