Type.GenericParameterPosition Właściwość

Definicja

Pobiera pozycję parametru typu na liście parametrów typu typu lub metody, która zadeklarowała parametr, gdy Type obiekt reprezentuje parametr typu typu lub metodę ogólną.

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

Wartość właściwości

Pozycja parametru typu na liście parametrów typu typu lub metody definiującej parametr. Liczby pozycji zaczynają się od 0.

Wyjątki

Bieżący typ nie reprezentuje parametru typu. Oznacza to, IsGenericParameter że zwraca wartość false.

Przykłady

W poniższym przykładzie zdefiniowano klasę ogólną z dwoma parametrami typu i zdefiniowano drugą klasę ogólną, która pochodzi od pierwszej klasy. Klasa bazowa klasy pochodnej ma dwa argumenty typu: pierwszy to Int32, a drugi jest parametrem typu pochodnego typu. W przykładzie są wyświetlane informacje o tych klasach ogólnych, w tym pozycje zgłaszane przez GenericParameterPosition właściwość .

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)
 */

Uwagi

Właściwość GenericParameterPosition zwraca pozycję parametru typu na liście parametrów definicji typu ogólnego lub definicji metody ogólnej, w której pierwotnie zdefiniowano parametr typu. Właściwości DeclaringType i DeclaringMethod identyfikują ogólny typ lub definicję metody:

  • DeclaringMethod Jeśli właściwość zwraca MethodInfowartość , która MethodInfo reprezentuje definicję metody ogólnej, a bieżący Type obiekt reprezentuje parametr typu tej definicji metody ogólnej.

  • DeclaringMethod Jeśli właściwość zwraca nullwartość , DeclaringType właściwość zawsze zwraca obiekt reprezentujący definicję Type typu ogólnego, a bieżący Type obiekt reprezentuje parametr typu tej definicji typu ogólnego.

Aby podać prawidłowy kontekst dla wartości GenericParameterPosition właściwości, należy zidentyfikować typ ogólny lub metodę, do którego należy parametr typu. Rozważmy na przykład wartość zwracaną metody GetSomething ogólnej w następującym kodzie:

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

Typ zwracany przez GetSomething element zależy od argumentów typu dostarczonych do klasy A i do GetSomething samego siebie. Możesz uzyskać element MethodInfo dla GetSomething, i , z którego można uzyskać typ zwracany. Podczas badania parametrów typu zwracanego typu GenericParameterPosition zwraca wartość 0 dla obu typów. Pozycja parametru V to 0, ponieważ V jest pierwszym parametrem typu na liście parametrów typu dla klasy A. Pozycja parametru X to 0, ponieważ X jest pierwszym parametrem typu na liście parametrów typu dla elementu GetSomething.

Uwaga

GenericParameterPosition Wywołanie właściwości powoduje wyjątek, jeśli bieżący Type nie reprezentuje parametru typu. Podczas badania argumentów typu otwartego typu typu należy użyć IsGenericParameter właściwości , aby określić, które parametry typu i które są typami. Właściwość IsGenericParameter zwraca true parametr typu. Następnie można użyć GenericParameterPosition metody , aby uzyskać jej pozycję i użyć DeclaringMethod właściwości i DeclaringType w celu określenia metody ogólnej lub definicji typu, która ją definiuje.

Dotyczy

Produkt Wersje
.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

Zobacz też