
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
generic<typename T, typename U> ref class B {};
generic<typename V, typename W> ref class D : B<int, V> {};
If you obtain a Type object representing 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), 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 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