|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Overview of Reflection and Generics
There are two keys to understanding how reflection handles generic types and methods:
The type parameters of generic type definitions and generic method definitions are represented by instances of theclass.
Many properties and methods of Type have different behavior when a Type object represents a generic type parameter. These differences are documented in the property and method topics. For example, seeand . In addition, some members are valid only when a Type object represents a generic type parameter. For example, see .
If an instance of Type represents a generic type, then it includes an array of types that represent the type parameters (for generic type definitions) or the type arguments (for constructed types). The same is true of an instance of theclass that represents a generic method.
Reflection provides methods of Type and MethodInfo that allow you to access the array of type parameters, and to determine whether an instance of Type represents a type parameter or an actual type.
For example code demonstrating the methods discussed here, see.
The following discussion assumes familiarity with the terminology of generics, such as the difference between type parameters and arguments and open or closed constructed types. For more information, see.
Is This a Generic Type or Method?
When you use reflection to examine an unknown type, represented by an instance of Type, use theproperty to determine whether the unknown type is generic. It returns true if the type is generic. Similarly, when you examine an unknown method, represented by an instance of the MethodInfo class, use the property to determine whether the method is generic.
Is This a Generic Type or Method Definition?
Use theproperty to determine whether a Type object represents a generic type definition, and use the method to determine whether a MethodInfo represents a generic method definition.
Generic type and method definitions are the templates from which instantiable types are created. Generic types in the .NET Framework class library, such as, are generic type definitions.
Is the Type or Method Open or Closed?
A generic type or method is closed if instantiable types have been substituted for all its type parameters, including all the type parameters of all enclosing types. You can only create an instance of a generic type if it is closed. Theproperty returns true if a type is open. For methods, the method performs the same function.
Generating Closed Generic Types
Once you have a generic type or method definition, use themethod to create a closed generic type or the method to create a MethodInfo for a closed generic method.
Getting the Generic Type or Method Definition
If you have an open generic type or method that is not a generic type or method definition, you cannot create instances of it and you cannot supply the type parameters that are missing. You must have a generic type or method definition. Use the GetGenericTypeDefinition method to obtain the generic type definition or themethod to obtain the generic method definition.
For example, if you have a Type object representing Dictionary<int, string> (Dictionary(Of Integer, String) in Visual Basic) and you want to create the type Dictionary<string, MyClass>, you can use the GetGenericTypeDefinition method to get a Type representing Dictionary<TKey, TValue> and then use the MakeGenericType method to produce a Type representing Dictionary<int, MyClass>.
For an example of an open generic type that is not a generic type, see "Type Parameter or Type Argument" later in this topic.
Examining Type Arguments and Type Parameters
Use themethod to obtain an array of Type objects that represent the type parameters or type arguments of a generic type, and use the method to do the same for a generic method.
Once you know that a Type object represents a type parameter, there are many additional questions reflection can answer. You can determine the type parameter's source, its position, and its constraints.
Type Parameter or Type Argument
To determine whether a particular element of the array is a type parameter or a type argument, use theproperty. The IsGenericParameter property is true if the element is a type parameter.
A generic type can be open without being a generic type definition, in which case it has a mixture of type arguments and type parameters. For example, in the following code, class D derives from a type created by substituting the first type parameter of D for the second type parameter of B.
If you obtain a Type object representing D<V, W> and use theproperty to obtain its base type, the resulting type B<int, V> is open, but it is not a generic type definition.
Source of a Generic Parameter
A generic type parameter might come from the type you are examining, from an enclosing type, or from a generic method. You can determine the source of the generic type parameter as follows:
First, use theproperty to determine whether the type parameter comes from a generic method. If the property value is not a null reference (Nothing in Visual Basic), then the source is a generic method.
If the source is not a generic method, use the DeclaringType property to determine the generic type the generic type parameter belongs to.
If the type parameter belongs to a generic method, the DeclaringType property returns the type that declared the generic method, which is irrelevant.
Position of a Generic Parameter
In rare situations, it is necessary to determine the position of a type parameter in the type parameter list of its declaring class. For example, suppose you have a Type object representing the B<int, V> type from the preceding example. The GetGenericArguments method gives you a list of type arguments, and when you examine V you can use the DeclaringMethod and DeclaringType properties to discover where it comes from. You can then use theproperty to determine its position in the type parameter list where it was defined. In this example, V is at position 0 (zero) in the type parameter list where it was defined.
Base Type and Interface Constraints
Use themethod to obtain the base type constraint and interface constraints of a type parameter. The order of the elements of the array is not significant. An element represents an interface constraint if it is an interface type.
Theproperty gets a value that indicates the special constraints. A type parameter can be constrained to be a reference type, a non-nullable value type, and to have a default constructor.
For a table of the invariant conditions for common terms in reflection for generic types, see System.Type.IsGenericType. For additional terms relating to generic methods, see System.Reflection.MethodInfo.IsGenericMethod.