I have always enjoyed the powers of reflection of .NET. Writing a small Search Functionality to work with Entity Framework as I explain in this blog post, spurred a lot of thinking and exploration.
Now I want to organize and make sense of the various properties that exist on a Type object, mostly the Boolean properties that describe several ‘characteristics’ of a .NET type:
I overview only the properties that pertain to generics, but I have created an interesting Excel sheet from which one can learn a few things, like typeof(int) is not a class, laid out sequentially and is sealed. You can find the sheet below.
This property indicates whether the type is generic or not, even if it is closed. For example
This property indicates whether the type has generic parameters at any level of the nesting. For example the Type object typeof(List<>) will return true for this property. For a nested generic type example:
Type t = typeof(List<>);
Type t2 = t.MakeGenericType(t);
The truth of this property means that you can create an instance of such a type, because it is partially open (has some generic slots somewhere for which a type hasn’t been specified, see ).
This property indicates whether this Type represents a type that is used as a placeholder for another type that would be provided for a generic type to make it concrete. You can only get such types using the GetGenericTypeArguments() methods on a generic type that has not been made concrete. Such a type would have as true all the following properties: IsGenericType, IsGenericTypeDefinition and ContainsGenericParameters. So having all three as true is equivalent to GetGenericArguments().Length > 0.
Generic Parameters have attributes associated with them as well as constraints. Get these using GenericParameterAttributes and GetGenericParameterConstraints respectively, calling these methods on methods for which IsGenericParameter is false would cause an exception to be thrown.
GenericParameterAttributes is a flag based enumeration that represents the kind of attributes associated with the generic parameter such as the availability of a default constructor and being a reference type (class constraint). You can check for the various cases (and combinations thereof) using a switch with bitwise operators, like here:
public class SomeClass
public static T MethodWithGenericParameterAttributes
where T : class
public static void Main(string args)
var genericMethod = typeof(SomeClass).GetMethod("MethodWithGenericParameterAttributes");
var T = genericMethod.GetGenericArguments();
var ggpc = T.GetGenericParameterConstraints();
var gpa = T.GenericParameterAttributes;
case GenericParameterAttributes.DefaultConstructorConstraint | GenericParameterAttributes.ReferenceTypeConstraint:
//reference type with a default constructor
This property indicates whether this type represents a definition of generic type, from which another Type can be constructed. You can construct concrete types from Generic Type Definitions, or partially closed types, as follows:
· Creating Concrete types from generic type definitions: happens when passing a concrete type to MakeGenericMethod of a generic type that is a generic type definition.
· Creating Partially closed types from generic type definitions: happens when passing a generic type that contains generic parameters to a generic type as an argument. This fills the generic parameter for the parent type, but still it will contain generic parameters. Here’s a code sample:
Type genericTypeDefinition = typeof(List<>);
Type partiallyClosedType = genericTypeDefinition.MakeGenericType(typeof(Dictionary<,>));
partiallyClosedType.ContainsGenericParameters will be true, so you can’t create instances from this type. partiallyClosedType.IsGenericTypeDefinition is false, so you can’t make a concrete type from partiallyClosedType. So is it useless? I think not.