Type.IsGenericType Type.IsGenericType Type.IsGenericType Type.IsGenericType Property

定義

現在の型がジェネリック型かどうかを示す値を取得します。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.

次のコード例の値を表示する、 IsGenericTypeIsGenericTypeDefinitionIsGenericParameter、および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
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

次の表に、クラスの作成および使用する例を示しますBaseDerived、および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:

IsGenericType falseためdは配列です。IsGenericType 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.

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

IsGenericTypeDefinitionfalseです。IsGenericTypeDefinition is false.

ContainsGenericParameters trueためTU、および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型の型パラメーターに割り当てられているためGします。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.

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 IsGenericType true場合でも、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.

適用対象

こちらもご覧ください