Type.GenericParameterPosition 속성

정의

Type 개체가 제네릭 형식이나 제네릭 메서드의 형식 매개 변수를 나타내는 경우, 해당 매개 변수가 선언된 제네릭 형식 또는 메서드의 형식 매개 변수 목록에서 해당 형식 매개 변수가 있는 위치를 가져옵니다.

public:
 abstract property int GenericParameterPosition { int get(); };
public:
 virtual property int GenericParameterPosition { int get(); };
public abstract int GenericParameterPosition { get; }
public virtual int GenericParameterPosition { get; }
member this.GenericParameterPosition : int
Public MustOverride ReadOnly Property GenericParameterPosition As Integer
Public Overridable ReadOnly Property GenericParameterPosition As Integer

속성 값

형식 매개 변수가 정의된 제네릭 형식 또는 메서드의 형식 매개 변수 목록에서 해당 형식 매개 변수가 있는 위치입니다. 위치 번호는 0부터 시작합니다.

예외

현재 형식이 형식 매개 변수를 나타내지 않습니다. 즉, IsGenericParameterfalse를 반환합니다.

예제

다음 예제에서는 두 개의 형식 매개 변수를 사용하여 제네릭 클래스를 정의하고 첫 번째 클래스에서 파생되는 두 번째 제네릭 클래스를 정의합니다. 파생 클래스의 기본 클래스에는 두 개의 형식 인수가 있습니다. 첫 번째는 이 Int32고, 두 번째 클래스는 파생 형식의 형식 매개 변수입니다. 이 예제에서는 속성에서 보고 GenericParameterPosition 한 위치를 포함하여 이러한 제네릭 클래스에 대한 정보를 표시합니다.

using namespace System;
using namespace System::Reflection;
using namespace System::Collections::Generic;

// Define a base class with two type parameters.
generic< class T,class U >
public ref class Base {};

// 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.
generic<class V>
public ref class Derived : Base<int,V> {};

public ref class Test
{
public:
    static void Main()
    {
        Console::WriteLine( 
            L"\r\n--- Display a generic type and the open constructed");
        Console::WriteLine(L"    type from which it is derived.");
      
        // Create a Type object representing the generic type definition
        // for the Derived type. Note the absence of type arguments.
        //
        Type^ derivedType = Derived::typeid;
        DisplayGenericTypeInfo(derivedType);
      
        // Display its open constructed base type.
        DisplayGenericTypeInfo(derivedType->BaseType);
    }


private:
    static void DisplayGenericTypeInfo(Type^ t)
    {
        Console::WriteLine(L"\r\n{0}", t);
        Console::WriteLine(L"\tIs this a generic type definition? {0}",
            t->IsGenericTypeDefinition);
        Console::WriteLine(L"\tIs it a generic type? {0}", t->IsGenericType);
        Console::WriteLine(L"\tDoes it have unassigned generic parameters? {0}",
            t->ContainsGenericParameters);
        if (t->IsGenericType)
        {
         
            // If this is a generic type, display the type arguments.
            //
            array<Type^>^typeArguments = t->GetGenericArguments();
            Console::WriteLine(L"\tList type arguments ({0}):",
                typeArguments->Length);
            System::Collections::IEnumerator^ myEnum = 
                typeArguments->GetEnumerator();
            while (myEnum->MoveNext())
            {
                Type^ tParam = safe_cast<Type^>(myEnum->Current);
            
                // IsGenericParameter is true only for generic type
                // parameters.
                //
                if (tParam->IsGenericParameter)
                {
                    Console::WriteLine( 
                        L"\t\t{0}  (unassigned - parameter position {1})", 
                        tParam, tParam->GenericParameterPosition);
                }
                else
                {
                    Console::WriteLine(L"\t\t{0}", tParam);
                }
            }
        }
    }
};

int main()
{
    Test::Main();
}

/* 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)
 */
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)
 */
open System
open System.Reflection
open System.Collections.Generic

// Define a base class with two type parameters.
type Base<'T, 'U>() = class end

// 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.
type Derived<'V>() = inherit Base<int, 'V>()

let displayGenericTypeInfo (t: Type) =
    printfn $"\n{t}"
    printfn $"\tIs this a generic type definition? {t.IsGenericTypeDefinition}"
    printfn $"\tIs it a generic type? {t.IsGenericType}"
    printfn $"\tDoes it have unassigned generic parameters? {t.ContainsGenericParameters}"

    if t.IsGenericType then
        // If this is a generic type, display the type arguments.
        let typeArguments = t.GetGenericArguments()

        printfn $"\tList type arguments ({typeArguments.Length}):"

        for tParam in typeArguments do
            // IsGenericParameter is true only for generic type
            // parameters.
            if tParam.IsGenericParameter then
                printfn $"\t\t{tParam}  (unassigned - parameter position {tParam.GenericParameterPosition})"
            else
                printfn $"\t\t{tParam}"

printfn $"\r\n--- Display a generic type and the open constructed"
printfn $"    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.) 
//
let derivedType = (typeof<Derived<_>>).GetGenericTypeDefinition()
displayGenericTypeInfo derivedType

// Display its open constructed base type.
displayGenericTypeInfo derivedType.BaseType

(* 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)
 *)
Imports System.Reflection
Imports System.Collections.Generic

' Define a base class with two type parameters.
Public Class Base(Of T, U)
End Class

' 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 uses 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 assigned type
' parameter and one unassigned type parameter.
Public Class Derived(Of V)
    Inherits Base(Of Integer, V)
End Class

Public Class Test
    
    Public Shared Sub Main() 
        Console.WriteLine(vbCrLf _
            & "--- 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.) 
        '
        Dim derivedType As Type = GetType(Derived(Of ))
        DisplayGenericTypeInfo(derivedType)
        
        ' Display its open constructed base type.
        DisplayGenericTypeInfo(derivedType.BaseType)
    
    End Sub
    
    Private Shared Sub DisplayGenericTypeInfo(ByVal t As Type) 
        Console.WriteLine(vbCrLf & "{0}", t)
        
        Console.WriteLine(vbTab & "Is this a generic type definition? " _
            & t.IsGenericTypeDefinition)
        
        Console.WriteLine(vbTab & "Is it a generic type? " _
            & t.IsGenericType)
        
        Console.WriteLine(vbTab _
            & "Does it have unassigned generic parameters? " _
            & t.ContainsGenericParameters)
        
        If t.IsGenericType Then
            ' If this is a generic type, display the type arguments.
            '
            Dim typeArguments As Type() = t.GetGenericArguments()
            
            Console.WriteLine(vbTab & "List type arguments (" _
                & typeArguments.Length & "):")
            
            For Each tParam As Type In typeArguments
                ' IsGenericParameter is true only for generic type
                ' parameters.
                '
                If tParam.IsGenericParameter Then
                    Console.WriteLine(vbTab & vbTab & tParam.ToString() _
                        & "  (unassigned - parameter position " _
                        & tParam.GenericParameterPosition & ")")
                Else
                    Console.WriteLine(vbTab & vbTab & tParam.ToString())
                End If
            Next tParam
        End If
    
    End Sub
End Class

' 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 parameters (2):
'                System.Int32
'                V  (unassigned - parameter position 0)
'

설명

속성은 GenericParameterPosition 형식 매개 변수가 원래 정의된 제네릭 형식 정의 또는 제네릭 메서드 정의의 매개 변수 목록에서 형식 매개 변수의 위치를 반환합니다. 및 DeclaringMethod 속성은 DeclaringType 제네릭 형식 또는 메서드 정의를 식별합니다.

  • 속성이 DeclaringMethod 제네릭 메서드 정의를 나타내는 를 MethodInfo 반환MethodInfo하고 현재 Type 개체가 해당 제네릭 메서드 정의의 형식 매개 변수를 나타내는 경우

  • 속성이 DeclaringMethod 를 반환 null하는 경우 속성은 DeclaringType 항상 제네릭 형식 정의를 나타내는 개체를 반환 Type 하고 현재 Type 개체는 해당 제네릭 형식 정의의 형식 매개 변수를 나타냅니다.

속성 값 GenericParameterPosition 에 대한 올바른 컨텍스트를 제공하려면 형식 매개 변수가 속한 제네릭 형식 또는 메서드를 식별해야 합니다. 예를 들어 다음 코드에서 제네릭 메서드 GetSomething 의 반환 값을 고려합니다.

generic<typename T, typename U> public ref class B { };
generic<typename V> public ref class A
{
public:
    generic<typename X> B<V, X>^ GetSomething()
    {
        return gcnew B<V, X>();
    }
};
public class B<T, U> { }
public class A<V>
{
    public B<V, X> GetSomething<X>()
    {
        return new B<V, X>();
    }
}
type B<'T, 'U>() = class end
type A<'V>() =
    member _.GetSomething<'X>() =
        B<'V, 'X>()
Public Class B(Of T, U)
End Class
Public Class A(Of V)
    Public Function GetSomething(Of X)() As B(Of V, X)
        Return New B(Of V, X)()
    End Function
End Class

에서 반환되는 GetSomething 형식은 클래스 A 및 자체에 제공된 형식 인수에 GetSomething 따라 달라집니다. 에 대한 GetSomethingMethodInfo 가져올 수 있으며, 에서 반환 형식을 가져올 수 있습니다. 반환 형식 GenericParameterPosition 의 형식 매개 변수를 검사할 때 둘 다에 대해 0을 반환합니다. 는 클래스A에 대한 형식 매개 변수 목록의 V 첫 번째 형식 매개 변수이므로 의 위치는 0 V 입니다. 의 위치 X 는 0 X 이므로 는 의 형식 매개 변수 목록에서 GetSomething첫 번째 형식 매개 변수입니다.

참고

현재 가 GenericParameterPosition 형식 매개 변수를 나타내지 않으면 속성을 호출하면 Type 예외가 발생합니다. 열린 생성된 형식의 형식 인수를 검사할 때 속성을 사용하여 IsGenericParameter 형식 매개 변수 및 형식을 알 수 있습니다. 속성은 IsGenericParameter 형식 매개 변수에 대해 를 반환 true 합니다. 그런 다음 메서드를 GenericParameterPosition 사용하여 해당 위치를 가져오고 및 DeclaringType 속성을 사용하여 DeclaringMethod 이를 정의하는 제네릭 메서드 또는 형식 정의를 확인할 수 있습니다.

적용 대상

추가 정보