Type.GetGenericTypeDefinition 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
현재 제네릭 형식을 생성할 수 있는 제네릭 형식 정의를 나타내는 Type 개체를 반환합니다.
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 개체입니다.
예외
현재 형식은 제네릭 형식이 아닙니다. 즉, IsGenericType이 false를 반환합니다.
호출된 메서드가 기본 클래스에서 지원되지 않습니다. 파생 클래스에서 구현을 제공해야 합니다.
예제
다음 코드 예제에서는 일반 인스턴스 만들기를 사용하여 생성된 형식의 인스턴스를 만든 다음 및 GetGenericTypeDefinition 메서드를 사용하여 GetType 생성된 형식 및 제네릭 형식 정의를 검색합니다. 이 예제에서는 제네릭 Dictionary<TKey,TValue> 형식을 사용합니다. 생성된 형식은 문자열 키가 있는 개체의 Test 을 Dictionary<TKey,TValue> 나타냅니다.
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
*/
open System
open System.Collections.Generic
type Test() = class end
let displayTypeInfo (t: Type) =
printfn $"\n{t}"
printfn $"\tIs this a generic type definition? {t.IsGenericTypeDefinition}"
printfn $"\tIs it a generic type? {t.IsGenericType}"
let typeArguments = t.GetGenericArguments()
printfn $"\tList type arguments ({typeArguments.Length}):"
for tParam in typeArguments do
printfn $"\t\t{tParam}"
printfn "\n--- Get the generic type that defines a constructed type."
// Create a Dictionary of Test objects, using strings for the keys.
let d = Dictionary<string, Test>()
// Get a Type object representing the constructed type.
let constructed = d.GetType()
displayTypeInfo constructed
let generic = constructed.GetGenericTypeDefinition()
displayTypeInfo generic
(* 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
'
설명
제네릭 형식 정의는 다른 형식을 생성할 수 있는 템플릿입니다. 예를 들어 제네릭 형식 정의 G<T> (C# 구문으로 표현됨, G(Of T) Visual Basic 또는 generic <typename T> ref class G C++)에서 형식 G<int> 을 생성하고 인스턴스화할 수 있습니다(G(Of Integer) Visual Basic의 경우). 이 생성된 형식을 Type 나타내는 개체가 지정되면 메서드는 GetGenericTypeDefinition 제네릭 형식 정의를 반환합니다.
동일한 형식 인수를 사용하여 동일한 제네릭 형식 정의에서 생성된 두 형식이 만들어지면 메서드는 GetGenericTypeDefinition 두 형식에 대해 동일한 Type 개체를 반환합니다.
이미 제네릭 형식 정의를 나타내는 개체에서 메서드를 Type 호출 GetGenericTypeDefinition 하면 현재 Type가 반환됩니다.
중요
제네릭 형식의 배열 자체는 제네릭이 아닙니다. C# 코드 또는 Visual Basic 코드 A<int>[] v;Dim v() As A(Of Integer)에서 변수 v 형식은 제네릭이 아닙니다. 를 호출GetGenericTypeDefinition하기 전에 를 사용하여 IsGenericType 형식이 제네릭인지 여부를 확인합니다.
제네릭 리플렉션에 사용되는 용어의 고정 조건 목록은 IsGenericType 속성 설명을 참조하세요.