Type.IsGenericType 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
현재 형식이 제네릭 형식인지를 나타내는 값을 가져옵니다.
public:
virtual property bool IsGenericType { bool get(); };
public virtual bool IsGenericType { get; }
member this.IsGenericType : bool
Public Overridable ReadOnly Property IsGenericType As Boolean
속성 값
true 현재 형식이 제네릭 형식이면 이고, 그렇지 않으면 입니다 false.
예제
다음 코드 예제에서는 설명 섹션에 설명된 형식의 IsGenericType, IsGenericTypeDefinition, IsGenericParameter및 ContainsGenericParameters 속성 값을 표시합니다. 속성 값에 대한 설명은 설명의 함께 제공되는 테이블을 참조하세요.
using namespace System;
using namespace System::Reflection;
generic<typename T, typename U> public ref class Base {};
generic<typename T> public ref class G {};
generic<typename V> public ref class Derived : Base<String^, V>
{
public:
G<Derived<V>^>^ F;
ref class Nested {};
};
void DisplayGenericType(Type^ t, String^ caption)
{
Console::WriteLine("\n{0}", caption);
Console::WriteLine(" Type: {0}", t);
Console::WriteLine("\t IsGenericType: {0}",
t->IsGenericType);
Console::WriteLine("\t IsGenericTypeDefinition: {0}",
t->IsGenericTypeDefinition);
Console::WriteLine("\tContainsGenericParameters: {0}",
t->ContainsGenericParameters);
Console::WriteLine("\t IsGenericParameter: {0}",
t->IsGenericParameter);
}
void main()
{
// Get the generic type definition for Derived, and the base
// type for Derived.
//
Type^ tDerived = Derived::typeid;
Type^ tDerivedBase = tDerived->BaseType;
// Declare an array of Derived<int>, and get its type.
//
array<Derived<int>^>^ d = gcnew array<Derived<int>^>(0);
Type^ tDerivedArray = d->GetType();
// Get a generic type parameter, the type of a field, and a
// type that is nested in Derived. Notice that in order to
// get the nested type it is necessary to either (1) specify
// the generic type definition Derived::typeid, as shown here,
// or (2) specify a type parameter for Derived.
//
Type^ tT = Base::typeid->GetGenericArguments()[0];
Type^ tF = tDerived->GetField("F")->FieldType;
Type^ tNested = Derived::Nested::typeid;
DisplayGenericType(tDerived, "generic<V> Derived");
DisplayGenericType(tDerivedBase, "Base type of generic<V> Derived");
DisplayGenericType(tDerivedArray, "Array of Derived<int>");
DisplayGenericType(tT, "Type parameter T from generic<T> Base");
DisplayGenericType(tF, "Field type, G<Derived<V>^>^");
DisplayGenericType(tNested, "Nested type in generic<V> Derived");
}
/* This code example produces the following output:
generic<V> Derived
Type: Derived`1[V]
IsGenericType: True
IsGenericTypeDefinition: True
ContainsGenericParameters: True
IsGenericParameter: False
Base type of generic<V> Derived
Type: Base`2[System.String,V]
IsGenericType: True
IsGenericTypeDefinition: False
ContainsGenericParameters: True
IsGenericParameter: False
Array of Derived<int>
Type: Derived`1[System.Int32][]
IsGenericType: False
IsGenericTypeDefinition: False
ContainsGenericParameters: False
IsGenericParameter: False
Type parameter T from generic<T> Base
Type: T
IsGenericType: False
IsGenericTypeDefinition: False
ContainsGenericParameters: True
IsGenericParameter: True
Field type, G<Derived<V>^>^
Type: G`1[Derived`1[V]]
IsGenericType: True
IsGenericTypeDefinition: False
ContainsGenericParameters: True
IsGenericParameter: False
Nested type in generic<V> Derived
Type: Derived`1+Nested[V]
IsGenericType: True
IsGenericTypeDefinition: True
ContainsGenericParameters: True
IsGenericParameter: False
*/
using System;
using System.Reflection;
public class Base<T, U> {}
public class Derived<V> : Base<string, V>
{
public G<Derived <V>> F;
public class Nested {}
}
public class G<T> {}
class Example
{
public static void Main()
{
// Get the generic type definition for Derived, and the base
// type for Derived.
//
Type tDerived = typeof(Derived<>);
Type tDerivedBase = tDerived.BaseType;
// Declare an array of Derived<int>, and get its type.
//
Derived<int>[] d = new Derived<int>[0];
Type tDerivedArray = d.GetType();
// Get a generic type parameter, the type of a field, and a
// type that is nested in Derived. Notice that in order to
// get the nested type it is necessary to either (1) specify
// the generic type definition Derived<>, as shown here,
// or (2) specify a type parameter for Derived.
//
Type tT = typeof(Base<,>).GetGenericArguments()[0];
Type tF = tDerived.GetField("F").FieldType;
Type tNested = typeof(Derived<>.Nested);
DisplayGenericType(tDerived, "Derived<V>");
DisplayGenericType(tDerivedBase, "Base type of Derived<V>");
DisplayGenericType(tDerivedArray, "Array of Derived<int>");
DisplayGenericType(tT, "Type parameter T from Base<T>");
DisplayGenericType(tF, "Field type, G<Derived<V>>");
DisplayGenericType(tNested, "Nested type in Derived<V>");
}
public static void DisplayGenericType(Type t, string caption)
{
Console.WriteLine("\n{0}", caption);
Console.WriteLine(" Type: {0}", t);
Console.WriteLine("\t IsGenericType: {0}",
t.IsGenericType);
Console.WriteLine("\t IsGenericTypeDefinition: {0}",
t.IsGenericTypeDefinition);
Console.WriteLine("\tContainsGenericParameters: {0}",
t.ContainsGenericParameters);
Console.WriteLine("\t IsGenericParameter: {0}",
t.IsGenericParameter);
}
}
/* This code example produces the following output:
Derived<V>
Type: Derived`1[V]
IsGenericType: True
IsGenericTypeDefinition: True
ContainsGenericParameters: True
IsGenericParameter: False
Base type of Derived<V>
Type: Base`2[System.String,V]
IsGenericType: True
IsGenericTypeDefinition: False
ContainsGenericParameters: True
IsGenericParameter: False
Array of Derived<int>
Type: Derived`1[System.Int32][]
IsGenericType: False
IsGenericTypeDefinition: False
ContainsGenericParameters: False
IsGenericParameter: False
Type parameter T from Base<T>
Type: T
IsGenericType: False
IsGenericTypeDefinition: False
ContainsGenericParameters: True
IsGenericParameter: True
Field type, G<Derived<V>>
Type: G`1[Derived`1[V]]
IsGenericType: True
IsGenericTypeDefinition: False
ContainsGenericParameters: True
IsGenericParameter: False
Nested type in Derived<V>
Type: Derived`1+Nested[V]
IsGenericType: True
IsGenericTypeDefinition: True
ContainsGenericParameters: True
IsGenericParameter: False
*/
open System
type Base<'T, 'U>() = class end
type G<'T>() = class end
type Derived<'V>() =
inherit Base<string, 'V>()
[<DefaultValue>]
val mutable public F : G<Derived<'V>>
let displayGenericType (t: Type) caption =
printfn $"\n{caption}"
printfn $" Type: {t}"
printfn $"\t IsGenericType: {t.IsGenericType}"
printfn $"\t IsGenericTypeDefinition: {t.IsGenericTypeDefinition}"
printfn $"\tContainsGenericParameters: {t.ContainsGenericParameters}"
printfn $"\t IsGenericParameter: {t.IsGenericParameter}"
// Get the generic type definition for Derived, and the base
// type for Derived.
let tDerived = typeof<Derived<_>>.GetGenericTypeDefinition()
let tDerivedBase = tDerived.BaseType
// Declare an array of Derived<int>, and get its type.
let d = Array.zeroCreate<Derived<int>> 0
let tDerivedArray = d.GetType()
// Get a generic type parameter, the type of a field, and a
// type that is nested in Derived. Notice that in order to
// get the nested type it is necessary to either (1) specify
// the generic type definition Derived<>, as shown here,
// or (2) specify a type parameter for Derived.
let tT = typeof<Base<_,_>>.GetGenericTypeDefinition().GetGenericArguments()[0]
let tF = tDerived.GetField("F").FieldType
displayGenericType tDerived "Derived<V>"
displayGenericType tDerivedBase "Base type of Derived<V>"
displayGenericType tDerivedArray "Array of Derived<int>"
displayGenericType tT "Type parameter T from Base<T>"
displayGenericType tF "Field type, G<Derived<V>>"
(* This code example produces the following output:
Derived<V>
Type: Derived`1[V]
IsGenericType: True
IsGenericTypeDefinition: True
ContainsGenericParameters: True
IsGenericParameter: False
Base type of Derived<V>
Type: Base`2[System.String,V]
IsGenericType: True
IsGenericTypeDefinition: False
ContainsGenericParameters: True
IsGenericParameter: False
Array of Derived<int>
Type: Derived`1[System.Int32][]
IsGenericType: False
IsGenericTypeDefinition: False
ContainsGenericParameters: False
IsGenericParameter: False
Type parameter T from Base<T>
Type: T
IsGenericType: False
IsGenericTypeDefinition: False
ContainsGenericParameters: True
IsGenericParameter: True
Field type, G<Derived<V>>
Type: G`1[Derived`1[V]]
IsGenericType: True
IsGenericTypeDefinition: False
ContainsGenericParameters: True
IsGenericParameter: False
*)
Imports System.Reflection
'
Public Class Base(Of T, U)
End Class
Public Class Derived(Of V)
Inherits Base(Of String, V)
Public F As G(Of Derived(Of V))
Public Class Nested
End Class
End Class
Public Class G(Of T)
End Class
Module Example
Sub Main
' Get the generic type definition for Derived, and the base
' type for Derived.
'
Dim tDerived As Type = GetType(Derived(Of ))
Dim tDerivedBase As Type = tDerived.BaseType
' Declare an array of Derived(Of Integer), and get its type.
'
Dim d(0) As Derived(Of Integer)
Dim tDerivedArray As Type = d.GetType()
' Get a generic type parameter, the type of a field, and a
' type that is nested in Derived. Notice that in order to
' get the nested type it is necessary to either (1) specify
' the generic type definition Derived(Of ), as shown here,
' or (2) specify a type parameter for Derived.
'
Dim tT As Type = GetType(Base(Of ,)).GetGenericArguments()(0)
Dim tF As Type = tDerived.GetField("F").FieldType
Dim tNested As Type = GetType(Derived(Of ).Nested)
DisplayGenericType(tDerived, "Derived(Of V)")
DisplayGenericType(tDerivedBase, "Base type of Derived(Of V)")
DisplayGenericType(tDerivedArray, "Array of Derived(Of Integer)")
DisplayGenericType(tT, "Type parameter T from Base(Of T)")
DisplayGenericType(tF, "Field type, G(Of Derived(Of V))")
DisplayGenericType(tNested, "Nested type in Derived(Of V)")
End Sub
Sub DisplayGenericType(ByVal t As Type, ByVal caption As String)
Console.WriteLine(vbLf & caption)
Console.WriteLine(" Type: {0}", t)
Console.WriteLine(vbTab & " IsGenericType: {0}", _
t.IsGenericType)
Console.WriteLine(vbTab & " IsGenericTypeDefinition: {0}", _
t.IsGenericTypeDefinition)
Console.WriteLine(vbTab & "ContainsGenericParameters: {0}", _
t.ContainsGenericParameters)
Console.WriteLine(vbTab & " IsGenericParameter: {0}", _
t.IsGenericParameter)
End Sub
End Module
' This code example produces the following output:
'
'Derived(Of V)
' Type: Derived`1[V]
' IsGenericType: True
' IsGenericTypeDefinition: True
' ContainsGenericParameters: True
' IsGenericParameter: False
'
'Base type of Derived(Of V)
' Type: Base`2[System.String,V]
' IsGenericType: True
' IsGenericTypeDefinition: False
' ContainsGenericParameters: True
' IsGenericParameter: False
'
'Array of Derived(Of Integer)
' Type: Derived`1[System.Int32][]
' IsGenericType: False
' IsGenericTypeDefinition: False
' ContainsGenericParameters: False
' IsGenericParameter: False
'
'Type parameter T from Base(Of T)
' Type: T
' IsGenericType: False
' IsGenericTypeDefinition: False
' ContainsGenericParameters: True
' IsGenericParameter: True
'
'Field type, G(Of Derived(Of V))
' Type: G`1[Derived`1[V]]
' IsGenericType: True
' IsGenericTypeDefinition: False
' ContainsGenericParameters: True
' IsGenericParameter: False
'
'Nested type in Derived(Of V)
' Type: Derived`1+Nested[V]
' IsGenericType: True
' IsGenericTypeDefinition: True
' ContainsGenericParameters: True
' IsGenericParameter: False
설명
사용 하 여는 IsGenericType 속성을 개체가 제네릭 형식을 Type 나타내는지 여부를 확인 합니다. 사용 하 여는 ContainsGenericParameters 속성을 개체가 열려 있는 생성 된 형식 또는 닫힌 생성 된 형식을 나타내는 여부를 Type 확인 합니다.
참고
IsGenericType 직접 실행 형식이 제네릭이 아니면 속성이 반환 false 됩니다. 예를 들어 요소가 형식 A<int> 인 배열(A(Of Integer) Visual Basic의 경우)은 제네릭 형식이 아닙니다.
다음 표에는 제네릭 리플렉션에 사용되는 일반적인 용어에 대한 고정 조건이 요약되어 있습니다.
| 용어 | 고정 |
|---|---|
| 제네릭 형식 정의(generic type definition) |
IsGenericTypeDefinition 속성은 true입니다.제네릭 형식을 정의합니다. 제네릭 형식 정의를 나타내는 개체에서 메서드를 Type 호출 MakeGenericType 하고 형식 인수의 배열을 지정하여 생성된 형식을 만듭니다. MakeGenericType 는 제네릭 형식 정의에서만 호출할 수 있습니다. 모든 제네릭 형식 정의는 제네릭 형식( IsGenericType 속성은 입니다 true)이지만 반대는 true가 아닙니다. |
| 제네릭 형식(generic type) |
IsGenericType 속성은 true입니다.제네릭 형식 정의, 개방형 생성 형식 또는 닫힌 생성 형식일 수 있습니다. 요소 형식이 제네릭인 배열 형식은 제네릭 형식이 아닙니다. 제네릭 형식에 대한 Type 포인터를 나타내는 개체도 마찬가지입니다. |
| 생성된 형식 열기 |
ContainsGenericParameters 속성은 true입니다.예를 들어 할당되지 않은 형식 매개 변수가 있는 제네릭 형식, 제네릭 형식 정의 또는 생성된 열린 형식에 중첩된 형식 또는 속성이 인 형식 인수가 있는 ContainsGenericParameters 제네릭 형식이 있습니다 true.개방형 생성 형식의 instance 만들 수 없습니다. 열려 있는 모든 생성된 형식이 제네릭인 것은 아닙니다. 예를 들어 요소 형식이 제네릭 형식 정의인 배열은 제네릭이 아니며 생성된 열린 형식에 대한 포인터는 제네릭이 아닙니다. |
| 닫힌 생성 형식 |
ContainsGenericParameters 속성은 false입니다.재귀적으로 검사할 때 형식에는 할당되지 않은 제네릭 매개 변수가 없습니다. |
| 제네릭 형식 매개 변수(generic type parameter) |
IsGenericParameter 속성은 true입니다.ContainsGenericParameters 속성은 true입니다.제네릭 형식 정의에서 나중에 할당될 형식의 자리 표시자입니다. |
| 제네릭 형식 인수(generic type argument) | 제네릭 형식 매개 변수를 포함하여 모든 형식일 수 있습니다. 형식 인수는 생성된 제네릭 형식을 만들 때 메서드에 MakeGenericType 전달되는 개체의 Type 배열로 지정됩니다. 결과 형식의 인스턴스를 만들 ContainsGenericParameters 려면 모든 형식 인수에 대한 속성이어야 false 합니다. |
다음 코드 예제와 표에서는 이러한 용어 및 고정 중 일부를 보여 줍니다. 클래스는 Derived 해당 기본 형식이 형식 인수 목록에 형식 및 형식 매개 변수가 혼합된 생성된 형식이므로 특히 중요합니다.
generic<typename T, typename U> public ref class Base {};
generic<typename T> public ref class G {};
generic<typename V> public ref class Derived : Base<String^, V>
{
public:
G<Derived<V>^>^ F;
ref class Nested {};
};
public class Base<T, U> {}
public class Derived<V> : Base<string, V>
{
public G<Derived <V>> F;
public class Nested {}
}
public class G<T> {}
type Base<'T, 'U>() = class end
type G<'T>() = class end
type Derived<'V>() =
inherit Base<string, 'V>()
[<DefaultValue>]
val mutable public F : G<Derived<'V>>
Public Class Base(Of T, U)
End Class
Public Class Derived(Of V)
Inherits Base(Of String, V)
Public F As G(Of Derived(Of V))
Public Class Nested
End Class
End Class
Public Class G(Of T)
End Class
다음 표에서는 클래스 , Derived및 G를 사용하고 빌드하는 예제를 보여 줍니다Base. C++ 및 C# 코드가 동일한 경우 하나의 항목만 표시됩니다.
| 예제 | 고정 |
|---|---|
Derived(Of V)Derived<V> |
이 형식의 경우: IsGenericType이(가) true인 경우IsGenericTypeDefinition이(가) true인 경우ContainsGenericParameters이(가) true인 경우 |
Base(Of String, V)Base<String,V>Base<String^,V> |
이 형식의 경우: IsGenericType이(가) true인 경우IsGenericTypeDefinition이(가) false인 경우ContainsGenericParameters이(가) true인 경우 |
Dim d() As Derived(Of Integer)Derived<int>[] d;array<Derived<int>^>^ d; |
변수 d형식의 경우:IsGenericType 는 false 배열이기 때문 d 입니다.IsGenericTypeDefinition이(가) false인 경우ContainsGenericParameters이(가) false인 경우 |
T, U및 V (표시되는 모든 곳) |
IsGenericParameter이(가) true인 경우IsGenericType 형식 매개 변수를 제네릭 형식으로 제한할 방법이 없기 때문입니다 false .IsGenericTypeDefinition이(가) false인 경우ContainsGenericParameters는 true , U및 V 가 제네릭 형식 매개 변수이기 때문T입니다. 이는 나중에 할당된 형식 인수에 대해 아무 것도 의미하지 않습니다. |
필드의 형식 F |
IsGenericType이(가) true인 경우IsGenericTypeDefinition 형식이 의 형식 매개 변수에 할당되었기 때문입니다 falseG. 이는 메서드를 호출 MakeGenericType 하는 것과 같습니다.ContainsGenericParameters 은 true 필드 F 형식에 개방형 생성 형식인 형식 인수가 있기 때문입니다. 생성된 형식은 형식 인수(즉, Base)가 제네릭 형식 정의이므로 열려 있습니다. 속성의 재귀 특성을 IsGenericType 보여 줍니다. |
중첩 클래스 Nested |
IsGenericType 는 true제네릭 형식에 Nested 중첩되어 있으므로 클래스에 고유한 제네릭 형식 매개 변수가 없더라도 입니다.IsGenericTypeDefinition이(가) true인 경우 즉, 메서드를 MakeGenericType 호출하고 바깥쪽 형식인 의 형식 Derived매개 변수를 제공할 수 있습니다.ContainsGenericParameters는 바깥쪽 형식인 에 Derived제네릭 형식 매개 변수가 있기 때문입니다true. 속성의 재귀 특성을 ContainsGenericParameters 보여 줍니다. |