C# TypeInfo GenericParameterPosition

Description

TypeInfo 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

TypeInfo.GenericParameterPosition has the following syntax.


public virtual int GenericParameterPosition { get; }

Example


using System;//from   w  w w.  j  a  v  a2s.  c  o  m
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);
        DisplayGenericTypeInfo(derivedType.BaseType);
    }

    private static void DisplayGenericTypeInfo(Type t)
    {
        if (t.IsGenericType)
        {
            Type[] typeArguments = t.GetGenericArguments();

            foreach (Type tParam in typeArguments)
            {
                // IsGenericParameter is true only for generic type 
                // parameters. 
                // 
                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.Reflection »




EventInfo
FieldInfo
MemberInfo
MethodInfo
ParameterInfo
TypeInfo