Type.GetGenericTypeDefinition 메서드

정의

현재 제네릭 형식을 생성할 수 있는 제네릭 형식 정의를 나타내는 Type 개체를 반환합니다.Returns a Type object that represents a generic type definition from which the current generic type can be constructed.

public:
 abstract Type ^ GetGenericTypeDefinition();
public:
 virtual Type ^ GetGenericTypeDefinition();
public abstract Type GetGenericTypeDefinition ();
public virtual Type GetGenericTypeDefinition ();
abstract member GetGenericTypeDefinition : unit -> Type
abstract member GetGenericTypeDefinition : unit -> Type
override this.GetGenericTypeDefinition : unit -> Type
Public MustOverride Function GetGenericTypeDefinition () As Type
Public Overridable Function GetGenericTypeDefinition () As Type

반환

Type

현재 형식을 생성할 수 있는 제네릭 형식을 나타내는 Type 개체입니다.A Type object representing a generic type from which the current type can be constructed.

예외

현재 형식은 제네릭 형식이 아닙니다.The current type is not a generic type. 즉, IsGenericTypefalse를 반환합니다.That is, IsGenericType returns false.

호출된 메서드가 기본 클래스에서 지원되지 않습니다.The invoked method is not supported in the base class. 파생 클래스에서 구현을 제공해야 합니다.Derived classes must provide an implementation.

예제

다음 코드 예제에서는 일반 인스턴스 생성을 사용 하 여 생성 된 형식의 인스턴스를 만든 다음 GetTypeGetGenericTypeDefinition 메서드를 사용 하 여 생성 된 형식과 제네릭 형식 정의를 검색 합니다.The following code example creates an instance of a constructed type by using ordinary instance creation and then uses the GetType and GetGenericTypeDefinition methods to retrieve the constructed type and the generic type definition. 이 예제에서는 제네릭 Dictionary<TKey,TValue> 형식을 사용 합니다. 생성 된 형식은 문자열 키가 있는 Test 개체의 Dictionary<TKey,TValue>를 나타냅니다.This example uses the generic Dictionary<TKey,TValue> type; the constructed type represents a Dictionary<TKey,TValue> of Test objects with string keys.

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

public ref class Test
{
public:
   static void Main()
   {
      Console::Write( L"\r\n--- Get the generic type that " );
      Console::WriteLine( L"defines a constructed type." );
     
      // Create a Dictionary of Test objects, using strings for the
      // keys.
      Dictionary< String^,Test^ >^ d = gcnew Dictionary< String^,Test^ >;
      
      // Get a Type object representing the constructed type.
      //
      Type^ constructed = d->GetType();
      DisplayTypeInfo( constructed );
      Type^ myGeneric = constructed->GetGenericTypeDefinition();
      DisplayTypeInfo( myGeneric );
   }

private:
   static void DisplayTypeInfo( 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 );
      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);
         Console::WriteLine( L"\t\t{0}", tParam );
      }
   }
};

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

/* This example produces the following output:

--- Get the generic type that defines a constructed type.

System.Collections.Generic.Dictionary`2[System.String,Test]
        Is this a generic type definition? False
        Is it a generic type? True
        List type arguments (2):
                System.String
                Test

System.Collections.Generic.Dictionary`2[TKey,TValue]
        Is this a generic type definition? True
        Is it a generic type? True
        List type arguments (2):
                TKey
                TValue
 */
using System;
using System.Reflection;
using System.Collections.Generic;

public class Test
{
    public static void Main()
    {
        Console.WriteLine("\r\n--- Get the generic type that defines a constructed type.");

        // Create a Dictionary of Test objects, using strings for the
        // keys.       
        Dictionary<string, Test> d = new Dictionary<string, Test>();

        // Get a Type object representing the constructed type.
        //
        Type constructed = d.GetType();
        DisplayTypeInfo(constructed);

        Type generic = constructed.GetGenericTypeDefinition();
        DisplayTypeInfo(generic);
    }

    private static void DisplayTypeInfo(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);
        Type[] typeArguments = t.GetGenericArguments();
        Console.WriteLine("\tList type arguments ({0}):", typeArguments.Length);
        foreach (Type tParam in typeArguments)
        {
            Console.WriteLine("\t\t{0}", tParam);
        }
    }
}

/* This example produces the following output:

--- Get the generic type that defines a constructed type.

System.Collections.Generic.Dictionary`2[System.String,Test]
        Is this a generic type definition? False
        Is it a generic type? True
        List type arguments (2):
                System.String
                Test

System.Collections.Generic.Dictionary`2[TKey,TValue]
        Is this a generic type definition? True
        Is it a generic type? True
        List type arguments (2):
                TKey
                TValue
 */
Imports System.Reflection
Imports System.Collections.Generic

Public Class Test
    Public Shared Sub Main() 
        Console.WriteLine(vbCrLf & "--- Get the generic type that defines a constructed type.")
        
        ' Create a Dictionary of Test objects, using strings for the
        ' keys.
        Dim d As New Dictionary(Of String, Test)
        
        ' Get a Type object representing the constructed type.
        '
        Dim constructed As Type = d.GetType()
        DisplayTypeInfo(constructed)
        
        Dim generic As Type = constructed.GetGenericTypeDefinition()
        DisplayTypeInfo(generic)
    End Sub
    
    Private Shared Sub DisplayTypeInfo(ByVal t As Type) 
        Console.WriteLine(vbCrLf & t.ToString())
        Console.WriteLine(vbTab & "Is this a generic type definition? " _
            & t.IsGenericTypeDefinition)
        Console.WriteLine(vbTab & "Is it a generic type? " _
            & t.IsGenericType)
        Dim typeArguments As Type() = t.GetGenericArguments()
        Console.WriteLine(vbTab & "List type arguments (" _
            & typeArguments.Length & "):")
        For Each tParam As Type In typeArguments
            Console.WriteLine(vbTab & vbTab & tParam.ToString())
        Next tParam
    End Sub
End Class

' This example produces the following output:
'
'--- Get the generic type that defines a constructed type.
'
'System.Collections.Generic.Dictionary`2[System.String,Test]
'        Is this a generic type definition? False
'        Is it a generic type? True
'        List type arguments (2):
'                System.String
'                Test
'
'System.Collections.Generic.Dictionary`2[TKey,TValue]
'        Is this a generic type definition? True
'        Is it a generic type? True
'        List type arguments (2):
'                TKey
'                TValue
' 

설명

제네릭 형식 정의 다른 형식을 생성 하는 데 사용 될 수 있는 템플릿입니다.A generic type definition is a template from which other types can be constructed. 예를 들어, 제네릭 형식 정의 G<T> (구문으로 C# 표현 됨)에서 (Visual Basic 또는의 C++generic <typename T> ref class G G(Of T)) 형식 G<int> (G(Of Integer))를 생성 하 고 인스턴스화할 수 있습니다.For example, from the generic type definition G<T> (expressed in C# syntax; G(Of T) in Visual Basic or generic <typename T> ref class G in C++) you can construct and instantiate the type G<int> (G(Of Integer) in Visual Basic). 이 생성 된 형식을 나타내는 Type 개체가 지정 된 경우 GetGenericTypeDefinition 메서드는 제네릭 형식 정의를 반환 합니다.Given a Type object representing this constructed type, the GetGenericTypeDefinition method returns the generic type definition.

동일한 형식 인수를 사용 하 여 두 개의 생성 된 형식이 동일한 제네릭 형식 정의에서 생성 되는 경우 GetGenericTypeDefinition 메서드는 두 형식 모두에 대해 동일한 Type 개체를 반환 합니다.If two constructed types are created from the same generic type definition, using the same type arguments, the GetGenericTypeDefinition method returns the same Type object for both types.

이미 제네릭 형식 정의를 나타내는 Type 개체에서 GetGenericTypeDefinition 메서드를 호출 하면 현재 Type반환 됩니다.If you call the GetGenericTypeDefinition method on a Type object that already represents a generic type definition, it returns the current Type.

중요

제네릭 형식의 배열 자체는 제네릭이 아닙니다.An array of generic types is not itself generic. C# 코드 A<int>[] v; 또는 Visual Basic 코드 Dim v() As A(Of Integer)에서 v 변수 형식은 제네릭이 아닙니다.In the C# code A<int>[] v; or the Visual Basic code Dim v() As A(Of Integer), the type of variable v is not generic. GetGenericTypeDefinition를 호출 하기 전에 IsGenericType를 사용 하 여 형식이 제네릭 인지 여부를 확인 합니다.Use IsGenericType to determine whether a type is generic before calling GetGenericTypeDefinition.

제네릭 리플렉션에 사용되는 용어의 고정 조건 목록은 IsGenericType 속성 설명을 참조하세요.For a list of the invariant conditions for terms used in generic reflection, see the IsGenericType property remarks.

적용 대상

추가 정보