Generics and Reflection (C# Programming Guide)

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 non-generic types. For more information, see Generics in the Run Time (C# Programming Guide).

In the .NET Framework 2.0 several new members are added to the Type class to enable run-time information for generic types. See the documentation on these classes for more information on how to use these methods and properties. 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.

System.Type Member Name

Description

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 have not 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 was not 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, new members are added to the MethodInfo class to 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.

System.Reflection.MemberInfo Member Name

Description

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 have not 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

Reference

Generics (C# Programming Guide)

Concepts

C# Programming Guide

Other Resources

Reflection and Generic Types

Generics in the .NET Framework