It really depends on what you mean by a generic type. In .NET a generic type
definition is used to refer to a type that contains one or more type parameters
that have not yet been assigned a value. An open constructed type is a type
that contains at least one type parameter that is not yet specified. A closed
constructed type is a type that has all type parameters assigned a value.
A generic type, in .NET, is a generic type definition, an open constructed type
or a closed constructed type.
The Type class exposes several properties to determine which of
the above conditions apply. The IsGenericType property is
true if the type represents any of the above cases. The IsGenericTypeDefinition
determines if a type is a generic type definition and therefore can not be instantiated.
The ContainsGenericParameter
property determines if the type is opened or closed. If it is true then the
type is open otherwise it is closed and can be instantiated.
The following code examines several different types to demonstrate the various states
of the properties.
class TestDictionary : Dictionary
{
}
class Program
{
static void PrintGenericInfo ( Type type )
{
Console.WriteLine("Type = " + type.FullName);
Console.WriteLine(" IsGenericType = " + type.IsGenericType);
Console.WriteLine(" IsGenericTypeDefinition = " + type.IsGenericTypeDefinition);
Console.WriteLine("Constructed type = {0}", type.ContainsGenericParameters ? "Open" : "Closed");
}
static void Main ( string[] args )
{
PrintGenericInfo(typeof(string)); // , false, false, closed
PrintGenericInfo(Type.GetType("System.Collections.ObjectModel.Collection`1")); // , true, true, open
PrintGenericInfo(new Collection<int>().GetType()); // ,true, false, closed
Type type = Type.GetType("ConsoleApplication1.TestDictionary`1");
PrintGenericInfo(type.BaseType); // ,true, false, open
}
}