Generics and reflection

Because the Common Language Runtime (CLR) has access to generic type information at run time, you can use reflection to obtain information about generic types in the same way as for nongeneric types. For more information, see Generics in the Runtime.

The System.Reflection.Emit namespace also contains new members that support generics. See How to: Define a Generic Type with Reflection Emit.

For a list of the invariant conditions for terms used in generic reflection, see the IsGenericType property remarks:

  • IsGenericType: Returns true if a type is generic.
  • GetGenericArguments: Returns an array of Type objects that represent the type arguments supplied for a constructed type, or the type parameters of a generic type definition.
  • GetGenericTypeDefinition: Returns the underlying generic type definition for the current constructed type.
  • GetGenericParameterConstraints: Returns an array of Type objects that represent the constraints on the current generic type parameter.
  • ContainsGenericParameters: Returns true if the type or any of its enclosing types or methods contain type parameters for which specific types haven't been supplied.
  • GenericParameterAttributes: Gets a combination of GenericParameterAttributes flags that describe the special constraints of the current generic type parameter.
  • GenericParameterPosition: For a Type object that represents a type parameter, gets the position of the type parameter in the type parameter list of the generic type definition or generic method definition that declared the type parameter.
  • IsGenericParameter: Gets a value that indicates whether the current Type represents a type parameter of a generic type or method definition.
  • IsGenericTypeDefinition: Gets a value that indicates whether the current Type represents a generic type definition, from which other generic types can be constructed. Returns true if the type represents the definition of a generic type.
  • DeclaringMethod: Returns the generic method that defined the current generic type parameter, or null if the type parameter wasn't defined by a generic method.
  • MakeGenericType: Substitutes the elements of an array of types for the type parameters of the current generic type definition, and returns a Type object representing the resulting constructed type.

In addition, members of the MethodInfo class enable run-time information for generic methods. See the IsGenericMethod property remarks for a list of invariant conditions for terms used to reflect on generic methods:

  • IsGenericMethod: Returns true if a method is generic.
  • GetGenericArguments: Returns an array of Type objects that represent the type arguments of a constructed generic method or the type parameters of a generic method definition.
  • GetGenericMethodDefinition: Returns the underlying generic method definition for the current constructed method.
  • ContainsGenericParameters: Returns true if the method or any of its enclosing types contain any type parameters for which specific types haven't been supplied.
  • IsGenericMethodDefinition: Returns true if the current MethodInfo represents the definition of a generic method.
  • MakeGenericMethod: Substitutes the elements of an array of types for the type parameters of the current generic method definition, and returns a MethodInfo object representing the resulting constructed method.

See also