Type.IsGenericType 속성

정의

현재 형식이 제네릭 형식인지를 나타내는 값을 가져옵니다.Gets a value indicating whether the current type is a generic type.

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합니다.true if the current type is a generic type; otherwise, false.

예제

다음 코드 예제에서는 설명 섹션에서 설명 하는 형식에 대 한 IsGenericType, IsGenericTypeDefinition, IsGenericParameterContainsGenericParameters 속성의 값을 표시 합니다.The following code example displays the value of the IsGenericType, IsGenericTypeDefinition, IsGenericParameter, and ContainsGenericParameters properties for the types described in the Remarks section. 속성 값에 대 한 설명에 함께 제공 되는 테이블을 참조 하세요.For explanations of the property values, see the accompanying table in Remarks.

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
 */
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 개체가 제네릭 형식을 나타내는지 여부를 확인 합니다.Use the IsGenericType property to determine whether a Type object represents a generic type. ContainsGenericParameters 속성을 사용 하 여 Type 개체가 개방형 생성 형식 또는 폐쇄형 생성 형식을 나타내는지 여부를 확인 합니다.Use the ContainsGenericParameters property to determine whether a Type object represents an open constructed type or a closed constructed type.

참고

직접 형식이 제네릭이 아닌 경우 IsGenericType 속성은 false을 반환 합니다.The IsGenericType property returns false if the immediate type is not generic. 예를 들어 요소가 A<int> 형식 (Visual Basic의A(Of Integer)) 인 배열은 자체가 제네릭 형식이 아닙니다.For example, an array whose elements are of type A<int> (A(Of Integer) in Visual Basic) is not itself a generic type.

다음 표에서 제네릭 리플렉션에 사용 되는 일반적인 용어에 대 한 고정 조건입니다.The following table summarizes the invariant conditions for common terms used in generic reflection.

용어Term 고정Invariant
제네릭 형식 정의(generic type definition)generic type definition IsGenericTypeDefinition 속성은 true입니다.The IsGenericTypeDefinition property is true.

제네릭 형식을 정의합니다.Defines a generic type. 생성 된 형식은 제네릭 형식 정의를 나타내고 형식 인수 배열을 지정 하는 Type 개체에 대해 MakeGenericType 메서드를 호출 하 여 생성 됩니다.A constructed type is created by calling the MakeGenericType method on a Type object that represents a generic type definition and specifying an array of type arguments.

MakeGenericType은 제네릭 형식 정의 에서만 호출할 수 있습니다.MakeGenericType can be called only on generic type definitions.

모든 제네릭 형식 정의는 제네릭 형식 (IsGenericType 속성이 true) 이지만 그 반대의 경우는 그렇지 않습니다.Any generic type definition is a generic type (the IsGenericType property is true), but the converse is not true.
제네릭 형식(generic type)generic type IsGenericType 속성은 true입니다.The IsGenericType property is true.

제네릭 형식 정의 개방형 생성된 형식, 또는 폐쇄형된 생성된 형식 수 있습니다.Can be a generic type definition, an open constructed type, or a closed constructed type.

배열 형식은 요소 형식이 제네릭 인지는 제네릭 형식 자체가 없습니다.Note that an array type whose element type is generic is not itself a generic type. 제네릭 형식에 대 한 포인터를 나타내는 Type 개체의 경우에도 마찬가지입니다.The same is true of a Type object representing a pointer to a generic type.
개방형 생성된 형식open constructed type ContainsGenericParameters 속성은 true입니다.The ContainsGenericParameters property is true.

예는 할당 되지 않은 형식 매개 변수, 제네릭 형식 정의 또는 개방형 생성 형식에 중첩 된 형식 또는 ContainsGenericParameters 속성이 true되는 형식 인수가 있는 제네릭 형식에 지정 된 형식 매개 변수를 포함 하는 제네릭 형식입니다.Examples are a generic type that has unassigned type parameters, a type that is nested in a generic type definition or in an open constructed type, or a generic type that has a type argument for which the ContainsGenericParameters property is true.

개방형 생성된 형식 인스턴스의 생성 하는 것이 불가능 합니다.It is not possible to create an instance of an open constructed type.

모든 열려 있지 않은 생성 된 형식 제네릭는 note 합니다.Note that not all open constructed types are generic. 예를 들어, 요소 형식이 제네릭 형식 정의 배열을 제네릭인 및 개방형 생성된 형식에 대 한 포인터는 제네릭이 아닙니다.For example, an array whose element type is a generic type definition is not generic, and a pointer to an open constructed type is not generic.
폐쇄형된 생성된 형식closed constructed type ContainsGenericParameters 속성은 false입니다.The ContainsGenericParameters property is false.

재귀적으로 검사 형식에 할당 되지 않은 제네릭 매개 변수가 없는 경우.When examined recursively, the type has no unassigned generic parameters.
제네릭 형식 매개 변수(generic type parameter)generic type parameter IsGenericParameter 속성은 true입니다.The IsGenericParameter property is true.

ContainsGenericParameters 속성은 true입니다.The ContainsGenericParameters property is true.

제네릭 형식 정의 나중에 할당할 수 있는 형식에 대 한 자리 표시자입니다.In a generic type definition, a placeholder for a type that will be assigned later.
제네릭 형식 인수(generic type argument)generic type argument 제네릭 형식 매개 변수를 포함 하 여 형식일 수 있습니다.Can be any type, including a generic type parameter.

형식 인수는 생성 된 제네릭 형식을 만들 때 MakeGenericType 메서드에 전달 되는 Type 개체의 배열로 지정 됩니다.Type arguments are specified as an array of Type objects passed to the MakeGenericType method when creating a constructed generic type. 결과 형식의 인스턴스를 만들 경우 모든 형식 인수에 대해 ContainsGenericParameters 속성을 false 해야 합니다.If instances of the resulting type are to be created, the ContainsGenericParameters property must be false for all the type arguments.

다음 코드 예제 및 테이블을 고정과 이러한 조건 중 일부를 보여 줍니다.The following code example and table illustrate some of these terms and invariants. Derived 클래스는 해당 기본 형식이 형식 인수 목록에 형식 및 형식 매개 변수를 혼합 하 여 생성 된 형식이 기 때문에 특히 중요 합니다.The Derived class is of particular interest because its base type is a constructed type that has a mixture of types and type parameters in its type argument list.

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> {}
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

다음 표에서는 Base, DerivedG클래스를 사용 하 여 빌드하는 예제를 보여 줍니다.The following table shows examples that use and build on the classes Base, Derived, and G. C + + 및 C# 코드가 동일 하면 항목이 하나만 표시 됩니다.When the C++ and C# code is the same, only one entry is shown.

예제Example 고정Invariants
Derived(Of V)

Derived<V>
이 유형의 경우:For this type:

IsGenericTypetrue인 경우IsGenericType is true.

IsGenericTypeDefinitiontrue인 경우IsGenericTypeDefinition is true.

ContainsGenericParameterstrue인 경우ContainsGenericParameters is true.
Base(Of String, V)

Base<String,V>

Base<String^,V>
이 유형의 경우:For this type:

IsGenericTypetrue인 경우IsGenericType is true.

IsGenericTypeDefinitionfalse인 경우IsGenericTypeDefinition is false.

ContainsGenericParameterstrue인 경우ContainsGenericParameters is true.
Dim d() As Derived(Of Integer)

Derived<int>[] d;

array<Derived<int>^>^ d;
변수의 형식 d:For the type of variable d:

d는 배열 이므로 IsGenericType false 됩니다.IsGenericType is false because d is an array.

IsGenericTypeDefinitionfalse인 경우IsGenericTypeDefinition is false.

ContainsGenericParametersfalse인 경우ContainsGenericParameters is false.
T, UV (표시 되는 모든 위치)T, U, and V (everywhere they appear) IsGenericParametertrue인 경우IsGenericParameter is true.

형식 매개 변수를 제네릭 형식으로 제한할 수 있는 방법이 없기 때문에 IsGenericType false 됩니다.IsGenericType is false because there is no way to constrain a type parameter to generic types.

IsGenericTypeDefinitionfalse인 경우IsGenericTypeDefinition is false.

T, UV 자체가 제네릭 형식 매개 변수 이므로 ContainsGenericParameters true 됩니다.ContainsGenericParameters is true because T, U, and V are themselves generic type parameters. 이 의미 하지 않습니다 나중에 할당 되는 형식 인수에 대 한 모든 것.This does not imply anything about type arguments that are assigned to them later.
필드의 형식 FThe type of field F IsGenericTypetrue인 경우IsGenericType is true.

G의 형식 매개 변수에 형식이 할당 되었으므로 IsGenericTypeDefinition false 됩니다.IsGenericTypeDefinition is false because a type has been assigned to the type parameter of G. 이는 MakeGenericType 메서드를 호출 하는 것과 같습니다.Note that this is equivalent to having called the MakeGenericType method.

F 필드의 형식에 개방형 생성 형식의 형식 인수가 있으므로 ContainsGenericParameters true 됩니다.ContainsGenericParameters is true because the type of field F has a type argument that is an open constructed type. 형식 인수 (Base)가 제네릭 형식 정의 이므로 생성 된 형식이 열려 있습니다.The constructed type is open because its type argument (that is, Base) is a generic type definition. 이는 IsGenericType 속성의 재귀적 특성을 보여 줍니다.This illustrates the recursive nature of the IsGenericType property.
중첩 된 클래스 NestedThe nested class Nested Nested 클래스에 제네릭 형식 매개 변수가 있는 경우에도 제네릭 형식에 중첩 되어 있으므로 IsGenericType true됩니다.IsGenericType is true, even though the Nested class has no generic type parameters of its own, because it is nested in a generic type.

IsGenericTypeDefinitiontrue인 경우IsGenericTypeDefinition is true. 즉, MakeGenericType 메서드를 호출 하 고 바깥쪽 형식의 형식 매개 변수 Derived를 제공할 수 있습니다.That is, you can call the MakeGenericType method and supply the type parameter of the enclosing type, Derived.

바깥쪽 형식 Derived에 제네릭 형식 매개 변수가 있기 때문에 ContainsGenericParameters true 됩니다.ContainsGenericParameters is true because the enclosing type, Derived, has generic type parameters. 이는 ContainsGenericParameters 속성의 재귀적 특성을 보여 줍니다.This illustrates the recursive nature of the ContainsGenericParameters property.

적용 대상

추가 정보