Type.GenericParameterPosition 屬性

定義

Type 物件表示泛型類型或泛型方法的類型參數時,在宣告參數的泛型類型或泛型方法之類型參數清單中,取得類型參數的位置。

C#
public abstract int GenericParameterPosition { get; }
C#
public virtual int GenericParameterPosition { get; }

屬性值

型別參數在宣告參數的泛型類型或方法之型別參數清單中的位置。 位置編號從 0 開始。

例外狀況

目前類型不代表類型參數。 亦即,IsGenericParameter 會傳回 false

範例

下列範例會定義具有兩個型別參數的泛型類別,並定義衍生自第一個類別的第二個泛型類別。 衍生類別的基類有兩個類型自變數:第一 Int32個是 ,而第二個則是衍生型別的類型參數。 此範例會顯示這些泛型類別的相關信息,包括 屬性所 GenericParameterPosition 報告的位置。

C#
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(
            "\r\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 definition 
        // for the Derived type, by omitting the type argument. (For
        // types with multiple type parameters, supply the commas but
        // omit the type arguments.) 
        //
        Type derivedType = typeof(Derived<>);
        DisplayGenericTypeInfo(derivedType);

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

    private static void DisplayGenericTypeInfo(Type t)
    {
        Console.WriteLine("\r\n{0}", t);

        Console.WriteLine("\tIs this a generic type definition? {0}", 
            t.IsGenericTypeDefinition);

        Console.WriteLine("\tIs it a generic type? {0}", 
            t.IsGenericType);

        Console.WriteLine("\tDoes it have unassigned generic parameters? {0}", 
            t.ContainsGenericParameters);

        if (t.IsGenericType)
        {
            // If this is a generic 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}  (unassigned - parameter position {1})",
                        tParam,
                        tParam.GenericParameterPosition);
                }
                else
                {
                    Console.WriteLine("\t\t{0}", tParam);
                }
            }
        }
    }
}

/* This example produces the following output:

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

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

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

備註

屬性會 GenericParameterPosition 傳回類型參數在泛型型別定義或泛型方法定義的參數清單中的位置,其中原本定義類型參數。 DeclaringTypeDeclaringMethod 屬性辨識泛型型:

若要提供屬性值的正確內容,您必須識別類型參數所屬的 GenericParameterPosition 泛型類型或方法。 例如,請考慮下列程式代碼中泛型方法 GetSomething 的傳回值:

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

GetSomething 傳回的類型取決於提供給類別 AGetSomething 本身的類型自變數。 您可以取得 MethodInfoGetSomething,並從中取得傳回型別。 當您檢查傳回型別的類型參數時, GenericParameterPosition 會針對這兩者傳回 0。 的位置 V 是 0,因為 V 是 類別 A之類型參數清單中的第一個類型參數。 的位置 X 是 0,因為 X 是 的類型參數清單中的 GetSomething第一個類型參數。

備註

GenericParameterPosition如果目前的 Type 不代表類型參數,則呼叫 屬性會造成例外狀況。 當您檢查開放式建構型別的類型自變數時,請使用 IsGenericParameter 屬性來告知哪些類型參數和類型為類型。 屬性 IsGenericParametertrue 傳回類型參數;然後 GenericParameterPosition 您可以使用 方法來取得其位置,並使用 DeclaringMethodDeclaringType 屬性來判斷定義它的泛型方法或型別定義。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另請參閱