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

プロパティ値

Boolean

true 現在の型がジェネリック型の場合は。それ以外の場合は falsetrue if the current type is a generic type; otherwise, false.

次のコード例では、「 IsGenericType IsGenericTypeDefinition IsGenericParameter 解説」で説明されている型の、、、およびの各プロパティの値を表示し ContainsGenericParameters ます。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> ( A(Of Integer) Visual Basic) である配列はそれ自体がジェネリック型ではありません。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 IsGenericTypeDefinition プロパティが true です。The IsGenericTypeDefinition property is true.

ジェネリック型を定義します。Defines a generic type. 構築された型は、 MakeGenericType ジェネリック型定義を表すオブジェクトに対してメソッドを呼び出し、型引数の配列を指定することによって作成され Type ます。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 ) ですが、逆の場合は true になりません。Any generic type definition is a generic type (the IsGenericType property is true), but the converse is not true.
ジェネリック型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 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 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 には、ジェネリック型パラメーターを含む任意の型を指定できます。Can be any type, including a generic type parameter.

型引数は、構築された Type ジェネリック型を作成するときにメソッドに渡されるオブジェクトの配列として指定され MakeGenericType ます。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 Derived G ます。The following table shows examples that use and build on the classes Base, Derived, and G. C++ と C# のコードが同じ場合は、1つのエントリのみが表示されます。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:

IsGenericTypefalse 配列であるためです dIsGenericType is false because d is an array.

IsGenericTypeDefinitionfalseです。IsGenericTypeDefinition is false.

ContainsGenericParametersfalseです。ContainsGenericParameters is false.
TU 、および V (すべてが表示されます)T, U, and V (everywhere they appear) IsGenericParametertrueです。IsGenericParameter is true.

IsGenericTypefalse 、型パラメーターをジェネリック型に制約する方法がないためです。IsGenericType is false because there is no way to constrain a type parameter to generic types.

IsGenericTypeDefinitionfalseです。IsGenericTypeDefinition is false.

ContainsGenericParameterstrue T 、、 U 、および V 自体がジェネリック型パラメーターであるためです。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.

IsGenericTypeDefinition は、 false の型パラメーターに型が割り当てられているためです GIsGenericTypeDefinition 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.

ContainsGenericParameters は、 true フィールドの型に、 F オープン構築型の型引数があるためです。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 IsGenericTypetrue 、ジェネリック型に入れ子になって Nested いるため、クラスに独自のジェネリック型パラメーターがない場合でも、です。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.

ContainsGenericParameters は、 true 外側の型に Derived ジェネリック型パラメーターがあるためです。ContainsGenericParameters is true because the enclosing type, Derived, has generic type parameters. これは、プロパティの再帰的な性質を示してい ContainsGenericParameters ます。This illustrates the recursive nature of the ContainsGenericParameters property.

適用対象

こちらもご覧ください