Mono Class Library: System.Type Overview | Members

System.Type.GenericParameterPosition Property

For a Type object that represents a type parameter of a generic type or generic method, gets the position of the type parameter in the type parameter list of the generic type or generic method.

public virtual int GenericParameterPosition { get; }

Value

A zero-based integer representing the position of a type parameter in the type parameter list of the generic type or generic method that declared the parameter.

Exceptions

TypeReason
InvalidOperationExceptionThe current type does not represent a type parameter. That is, Type.IsGenericParameter returns false.

Remarks

This read-only property returns the position of a type parameter in the parameter list of the generic type definition or generic method definition where the type parameter was originally defined. The Type.DeclaringType and System.Type.DeclaringMethod properties identify the generic type or method definition:

To provide the correct context for the value of the Type.GenericParameterPosition property, it is necessary to identify the generic type or method a type parameter belongs to. For example, consider the return value of the generic method GetSomething in the following C# code:

Example

public class B<T, U> { }
 public class A<V>
 {
     public B<V, X> GetSomething<X>()
     {
         return new Base<V, X>();
     }
 }

The type returned by GetSomething depends on the type arguments supplied to class A and GetSomething itself. You can obtain a System.Reflection.MethodInfo for GetSomething and from that you can obtain the return type. When you examine the type parameters of the return type, Type.GenericParameterPosition returns zero for both. The position of V is zero because V is the first type parameter in the type parameter list for class A. The position ofX is zero because X is the first type parameter in the type parameter list for GetSomething.

Note:

Calling the Type.GenericParameterPosition property causes an exception if the current Type does not represent a type parameter. When you examine the type arguments of an open constructed type, use the Type.IsGenericParameter property to tell which are type parameters and which are types. The Type.IsGenericParameter property returnstrue for a type parameter; you can then use the Type.GenericParameterPosition method to obtain its position, and the Type.DeclaringMethod and Type.DeclaringType properties to determine the generic method or type definition that defines it.

Example

The following example defines a generic class with two type parameters, and a generic class that derives from it. The base class of the derived type has one unbound type parameter and one type parameter bound to int. The example displays information about these generic classes, including the positions reported by Type.GenericParameterPosition.

C# Example

using System;
using System.Reflection;
using System.Collections.Generic;
// Define a base class with two type parameters.
public class Base<T, U> { }

// Define a derived class. The derived class inherits from a constructed
// class that meets the following criteria:
//   (1) Its generic type definition is Base<T, U>.
//   (2) It specifies int for the first type parameter.
//   (3) For the second type parameter, it uses the same type that is used
//       for the type parameter of the derived class.
// Thus, the derived class is a generic type with one type parameter, but
// its base class is an open constructed type with one type argument and
// one type parameter.
public class Derived<V>: Base<int,V> { }

public class Test
{
	public static void Main()
	{
		Console.WriteLine("\n--- Display a generic type and the open constructed");
		Console.WriteLine("    type from which it is derived.");

		// Create a Type object representing the generic type Derived.
		//
		Type derivedType = Type.GetType("Derived");

		DisplayGenericTypeInfo(derivedType);

		// Display its open constructed base type.
		DisplayGenericTypeInfo(derivedType.BaseType);
	}

	private static void DisplayGenericTypeInfo(Type t)
	{
		Console.WriteLine("\n{0}", t);
		Console.WriteLine("\tIs this a generic type definition? {0}", t.IsGenericTypeDefinition);
		Console.WriteLine("\tDoes it have generic arguments? {0}", t.HasGenericArguments);
		Console.WriteLine("\tDoes it have unbound generic parameters? {0}", t.ContainsGenericParameters);
		if (t.HasGenericArguments)
		{
			// If the type is a generic type definition or a 
			// constructed type, display the type arguments.
			//
			Type[] typeArguments = t.GetGenericArguments();

			Console.WriteLine("\tList type arguments ({0}):", typeArguments.Length);
			foreach (Type tParam in typeArguments)
			{
				// IsGenericParameter is true only for generic type
				// parameters.
				//
				if (tParam.IsGenericParameter)
				{
					Console.WriteLine("\t\t{0}  (unbound - parameter position {1})", tParam, tParam.GenericParameterPosition);
				}
				else
				{
					Console.WriteLine("\t\t{0}", tParam);
				}
			}
		}
		else
		{
			Console.WriteLine("\tThis is not a generic or constructed type.");
		}
	}
}

/* This example produces the following output:

--- Display a generic type and the open constructed
    type from which it is derived.

Derived[V]
        Is this a generic type definition? True
        Does it have generic arguments? True
        Does it have unbound generic parameters? True
        List type arguments (1):
                V  (unbound - parameter position 0)

Base[System.Int32, V]
        Is this a generic type definition? False
        Does it have generic arguments? True
        Does it have unbound generic parameters? True
        List type arguments (2):
                System.Int32
                V  (unbound - parameter position 0)
 */

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 2.0.0.0
Since: .NET 2.0