C# Type GenericParameterPosition

Description

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

Syntax

Type.GenericParameterPosition has the following syntax.


public virtual int GenericParameterPosition { get; }

Example

The following example defines a generic class with two type parameters and defines a second generic class that derives from the first class.


using System;//from w  ww.  j a  v  a2 s . c om
using System.Reflection;
using System.Collections.Generic;

public class Base<T, U> { }

public class Derived<V> : Base<int, V> { }

public class Test
{
    public static void Main()
    {
        Type derivedType = typeof(Derived<>);
        DisplayGenericTypeInfo(derivedType);
    }

    private static void DisplayGenericTypeInfo(Type t)
    {

        if (t.IsGenericType)
        {
            Type[] typeArguments = t.GetGenericArguments();
            foreach (Type tParam in typeArguments)
            {
                if (tParam.IsGenericParameter)
                {
                    Console.WriteLine(
                        "\t\t{0}  (unassigned - parameter position {1})",
                        tParam,
                        tParam.GenericParameterPosition);
                }
                else
                {
                    Console.WriteLine("\t\t{0}", tParam);
                }
            }
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version