Type.IsGenericType Propriété

Définition

Obtient une valeur indiquant si le type actuel est un type générique.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

Valeur de propriété

trueSi le type actuel est un type générique ; Sinon, false.true if the current type is a generic type; otherwise, false.

Exemples

L’exemple de code suivant affiche la valeur des IsGenericTypepropriétés IsGenericTypeDefinition, IsGenericParameter, et ContainsGenericParameters pour les types décrits dans la section Notes.The following code example displays the value of the IsGenericType, IsGenericTypeDefinition, IsGenericParameter, and ContainsGenericParameters properties for the types described in the Remarks section. Pour obtenir des explications sur les valeurs de propriété, consultez le tableau qui l’accompagne dans la section Notes.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

Remarques

Utilisez la IsGenericType propriété pour déterminer si un Type objet représente un type générique.Use the IsGenericType property to determine whether a Type object represents a generic type. Utilisez la ContainsGenericParameters propriété pour déterminer si un Type objet représente un type construit ouvert ou un type construit fermé.Use the ContainsGenericParameters property to determine whether a Type object represents an open constructed type or a closed constructed type.

Notes

La IsGenericType propriété retourne false si le type immédiat n’est pas générique.The IsGenericType property returns false if the immediate type is not generic. Par exemple, un tableau dont les éléments sont de A<int> typeA(Of Integer) (en Visual Basic) n’est pas lui-même un type générique.For example, an array whose elements are of type A<int> (A(Of Integer) in Visual Basic) is not itself a generic type.

Le tableau suivant récapitule les conditions d’invariant pour les termes courants utilisés dans la réflexion générique.The following table summarizes the invariant conditions for common terms used in generic reflection.

TermeTerm InvariantInvariant
définition de type génériquegeneric type definition La propriété IsGenericTypeDefinition est true.The IsGenericTypeDefinition property is true.

Définit un type générique.Defines a generic type. Un type construit est créé en appelant la MakeGenericType méthode sur un Type objet qui représente une définition de type générique et en spécifiant un tableau d’arguments de 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.

MakeGenericTypepeut être appelé uniquement sur les définitions de type générique.MakeGenericType can be called only on generic type definitions.

Toute définition de type générique est un type générique ( IsGenericType la propriété truea la valeur), mais l’inverse n’est pas vrai.Any generic type definition is a generic type (the IsGenericType property is true), but the converse is not true.
type génériquegeneric type La propriété IsGenericType est true.The IsGenericType property is true.

Peut être une définition de type générique, un type construit ouvert ou un type construit fermé.Can be a generic type definition, an open constructed type, or a closed constructed type.

Notez qu’un type de tableau dont le type d’élément est générique n’est pas lui-même un type générique.Note that an array type whose element type is generic is not itself a generic type. Il en va de même pour Type un objet représentant un pointeur vers un type générique.The same is true of a Type object representing a pointer to a generic type.
type construit ouvertopen constructed type La propriété ContainsGenericParameters est true.The ContainsGenericParameters property is true.

Les exemples sont un type générique qui a des paramètres de type non assignés, un type qui est imbriqué dans une définition de type générique ou dans un type construit ouvert, ou un type générique qui a un argument de ContainsGenericParameters type pour truelequel la propriété est.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.

Il n’est pas possible de créer une instance d’un type construit ouvert.It is not possible to create an instance of an open constructed type.

Notez que tous les types construits ouverts ne sont pas génériques.Note that not all open constructed types are generic. Par exemple, un tableau dont le type d’élément est une définition de type générique n’est pas générique et un pointeur vers un type construit ouvert n’est pas générique.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.
type construit ferméclosed constructed type La propriété ContainsGenericParameters est false.The ContainsGenericParameters property is false.

Lorsqu’il est examiné de manière récursive, le type n’a aucun paramètre générique non assigné.When examined recursively, the type has no unassigned generic parameters.
paramètre de type génériquegeneric type parameter La propriété IsGenericParameter est true.The IsGenericParameter property is true.

La propriété ContainsGenericParameters est true.The ContainsGenericParameters property is true.

Dans une définition de type générique, espace réservé pour un type qui sera affecté ultérieurement.In a generic type definition, a placeholder for a type that will be assigned later.
argument de type génériquegeneric type argument Peut être n’importe quel type, y compris un paramètre de type générique.Can be any type, including a generic type parameter.

Les arguments de type sont spécifiés sous Type la forme d’un MakeGenericType tableau d’objets passés à la méthode lors de la création d’un type générique construit.Type arguments are specified as an array of Type objects passed to the MakeGenericType method when creating a constructed generic type. Si des instances du type résultant doivent être créées, la ContainsGenericParameters propriété doit être false pour tous les arguments de type.If instances of the resulting type are to be created, the ContainsGenericParameters property must be false for all the type arguments.

L’exemple de code et le tableau ci-dessous illustrent certains de ces termes et invariants.The following code example and table illustrate some of these terms and invariants. La Derived classe présente un intérêt particulier, car son type de base est un type construit qui a un mélange de types et de paramètres de type dans sa liste d’arguments de type.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

Le tableau suivant présente des exemples qui utilisent et s’appuient Basesur Derivedles classes G, et.The following table shows examples that use and build on the classes Base, Derived, and G. Lorsque le C++ code C# et est identique, une seule entrée est affichée.When the C++ and C# code is the same, only one entry is shown.

ExempleExample InvariantsInvariants
Derived(Of V)

Derived<V>
Pour ce type :For this type:

IsGenericType a la valeur true.IsGenericType is true.

IsGenericTypeDefinition est true.IsGenericTypeDefinition is true.

ContainsGenericParameters a la valeur true.ContainsGenericParameters is true.
Base(Of String, V)

Base<String,V>

Base<String^,V>
Pour ce type :For this type:

IsGenericType a la valeur true.IsGenericType is true.

IsGenericTypeDefinition a la valeur false.IsGenericTypeDefinition is false.

ContainsGenericParameters a la valeur true.ContainsGenericParameters is true.
Dim d() As Derived(Of Integer)

Derived<int>[] d;

array<Derived<int>^>^ d;
Pour le type de variable d:For the type of variable d:

IsGenericTypeest falsed au fait que est un tableau.IsGenericType is false because d is an array.

IsGenericTypeDefinition est false.IsGenericTypeDefinition is false.

ContainsGenericParameters est false.ContainsGenericParameters is false.
T, U etV (partout où ils apparaissent)T, U, and V (everywhere they appear) IsGenericParameter est true.IsGenericParameter is true.

IsGenericTypeest false dû au fait qu’il n’existe aucun moyen de contraindre un paramètre de type à des types génériques.IsGenericType is false because there is no way to constrain a type parameter to generic types.

IsGenericTypeDefinition a la valeur false.IsGenericTypeDefinition is false.

ContainsGenericParametersest trueTau Ufait que V , et sont eux-mêmes des paramètres de type générique.ContainsGenericParameters is true because T, U, and V are themselves generic type parameters. Cela n’implique rien sur les arguments de type qui leur sont assignés ultérieurement.This does not imply anything about type arguments that are assigned to them later.
Type de champFThe type of field F IsGenericType a la valeur true.IsGenericType is true.

IsGenericTypeDefinitionest false dû au fait qu’un type a été assigné au paramètre Gde type de.IsGenericTypeDefinition is false because a type has been assigned to the type parameter of G. Notez que cela équivaut à appeler la MakeGenericType méthode.Note that this is equivalent to having called the MakeGenericType method.

ContainsGenericParametersest true dû au fait que le F type de champ a un argument de type qui est un type construit ouvert.ContainsGenericParameters is true because the type of field F has a type argument that is an open constructed type. Le type construit est ouvert, car son argument de type (autrement Basedit,) est une définition de type générique.The constructed type is open because its type argument (that is, Base) is a generic type definition. Cela illustre la nature récursive de la IsGenericType propriété.This illustrates the recursive nature of the IsGenericType property.
La classe imbriquéeNestedThe nested class Nested IsGenericTypeest true, même si la Nested classe n’a pas de paramètres de type générique propre, parce qu’elle est imbriquée dans un type générique.IsGenericType is true, even though the Nested class has no generic type parameters of its own, because it is nested in a generic type.

IsGenericTypeDefinition est true.IsGenericTypeDefinition is true. Autrement dit, vous pouvez appeler la MakeGenericType méthode et fournir le paramètre de type du type englobant, Derived.That is, you can call the MakeGenericType method and supply the type parameter of the enclosing type, Derived.

ContainsGenericParametersest true dû au fait que le type Derivedenglobant,, a des paramètres de type générique.ContainsGenericParameters is true because the enclosing type, Derived, has generic type parameters. Cela illustre la nature récursive de la ContainsGenericParameters propriété.This illustrates the recursive nature of the ContainsGenericParameters property.

S’applique à

Voir aussi