다음을 통해 공유


Overview of Reflection and Generics

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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 the Type class.

    NoteNote:

    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, see IsAutoClass and DeclaringType. In addition, some members are valid only when a Type object represents a generic type parameter. For example, see GetGenericTypeDefinition.

  • If an instance of Type represents a generic type, 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 the MethodInfo class that represents a generic method.

Reflection provides methods of Type and MethodInfo that enable 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 How to: Examine and Instantiate Generic Types with Reflection.

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 Generics Overview.

Is This a Generic Type or Method?

When you use reflection to examine an unknown type, represented by an instance of Type, use the IsGenericType property to determine whether the unknown type is generic. It returns true if the type is generic. Similarly, when you examine an unknown method, which is represented by an instance of the MethodInfo class, use the IsGenericMethod property to determine whether the method is generic.

Is This a Generic Type or Method Definition?

Use the IsGenericTypeDefinition property to determine whether a Type object represents a generic type definition, and use the IsGenericMethodDefinition property 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 Dictionary<TKey, TValue>, 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. The Type.ContainsGenericParameters property returns true if a type is open. For methods, the MethodInfo.ContainsGenericParameters property performs the same function.

Generating Closed Generic Types

Once you have a generic type or method definition, use the MakeGenericType method to create a closed generic type or the MakeGenericMethod 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 the GetGenericMethodDefinition method to obtain the generic method definition.

For example, if you have a Type object that represents 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 the Type.GetGenericArguments method to obtain an array of Type objects that represent the type parameters or type arguments of a generic type, and use the MethodInfo.GetGenericArguments 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 the IsGenericParameter property. 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.

class B<T, U> {}
class D<V, W> : B<int, V> {}
Class B(Of T, U)
End Class
Class D(Of V, W)
    Inherits B(Of Integer, V)
End Class

If you obtain a Type object that represents D<V, W> and use the BaseType property 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 the DeclaringMethod property to determine whether the type parameter comes from a generic method. If the property value is not a null reference (Nothing in Visual Basic), 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 that represents 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 the GenericParameterPosition property 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 the GetGenericParameterConstraints method 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.

Special Constraints

The GenericParameterAttributes property gets a GenericParameterAttributes value that indicates the special constraints. A type parameter can be constrained to be a reference type, to be a non-nullable value type, and to have a default constructor.

Invariants

For a table of the invariant conditions for common terms in reflection for generic types, see Type.IsGenericType. For additional terms relating to generic methods, see MethodInfo.IsGenericMethod.