I would like to perform a test to check if an object is of a generic type. I’ve tried the following without success:
public bool Test()
{
List<int> list = new List<int>();
return list.GetType() == typeof(List<>);
}
What am I doing wrong and how can I perform this test?
If you want to check if it’s an instance of a generic type:
If you want to check if it’s a generic
List<T>:As Jon points out, this checks the exact type equivalence. Returning
falsedoesn’t necessarily meanlist is List<T>returnsfalse(i.e. the object cannot be assigned to aList<T>variable).