Cet article a fait l'objet d'une traduction manuelle. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte.
Traduction
Source
Ce sujet n'a pas encore été évalué - Évaluez ce sujet

Type.IsGenericTypeDefinition, propriété

Obtient une valeur qui indique si le Type actuel représente une définition de type générique, à partir de laquelle d'autres types génériques peuvent être construits.

Espace de noms :  System
Assembly :  mscorlib (dans mscorlib.dll)
public virtual bool IsGenericTypeDefinition { get; }

Valeur de propriété

Type : System.Boolean
true si l'objet Type représente une définition de type générique ; sinon, false.

Une définition de type générique est un modèle à partir duquel d'autres types peuvent être construits. Par exemple, à partir de la définition de type générique G<T> (exprimé dans la syntaxe C# ; G(Of T) en Visual Basic ou generic <typename T> ref class G en C++) vous pouvez construire et instancier le type G<int> (G(Of Integer) en Visual Basic), en appelant la méthode MakeGenericType avec une liste d'arguments génériques contenant le type Int32. Disposant d'un objet Type représentant ce type construit, la méthode GetGenericTypeDefinition obtient en retour la définition de type générique.

Utilisez la propriété IsGenericTypeDefinition pour déterminer si vous pouvez créer de nouveaux types à partir du type actuel. Si la propriété IsGenericTypeDefinition retourne true, vous pouvez appeler la méthode MakeGenericType pour créer de nouveaux types génériques.

Pour obtenir la liste des conditions invariables des termes utilisés dans une réflexion générique, consultez les notes sur la propriété IsGenericType.

L'exemple suivant affiche des informations sur un type, y compris s'il s'agit d'une définition de type générique ou non. Les informations sont affichées pour un type construit, pour sa définition de type générique et pour un type ordinaire.


using System;
using System.Reflection;
using System.Collections.Generic;

public class Test
{
    private static void DisplayGenericTypeInfo(Type t)
    {
        Console.WriteLine("\r\n{0}", t);

        Console.WriteLine("\tIs this a generic type definition? {0}", 
            t.IsGenericTypeDefinition);

        Console.WriteLine("\tIs it a generic type? {0}", 
            t.IsGenericType);

        if (t.IsGenericType)
        {
            // If this is a generic type, display the type arguments.
            //
            Type[] typeArguments = t.GetGenericArguments();

            Console.WriteLine("\tList type arguments ({0}):", 
                typeArguments.Length);

            foreach (Type tParam in typeArguments)
            {
                // If this is a type parameter, display its
                // position.
                //
                if (tParam.IsGenericParameter)
                {
                    Console.WriteLine("\t\t{0}\t(unassigned - parameter position {1})",
                        tParam,
                        tParam.GenericParameterPosition);
                }
                else
                {
                    Console.WriteLine("\t\t{0}", tParam);
                }
            }
        }
    }

    public static void Main()
    {
        Console.WriteLine("\r\n--- Display information about a constructed type, its");
        Console.WriteLine("    generic type definition, and an ordinary type.");

        // Create a Dictionary of Test objects, using strings for the
        // keys.       
        Dictionary<string, Test> d = new Dictionary<string, Test>();

        // Display information for the constructed type and its generic
        // type definition.
        DisplayGenericTypeInfo(d.GetType());
        DisplayGenericTypeInfo(d.GetType().GetGenericTypeDefinition());

        // Display information for an ordinary type.
        DisplayGenericTypeInfo(typeof(string));
    }
}

/* This example produces the following output:

--- Display information about a constructed type, its
    generic type definition, and an ordinary type.

System.Collections.Generic.Dictionary[System.String,Test]
        Is this a generic type definition? False
        Is it a generic type? True
        List type arguments (2):
                System.String
                Test

System.Collections.Generic.Dictionary[TKey,TValue]
        Is this a generic type definition? True
        Is it a generic type? True
        List type arguments (2):
                TKey    (unassigned - parameter position 0)
                TValue  (unassigned - parameter position 1)

System.String
        Is this a generic type definition? False
        Is it a generic type? False
 */


.NET Framework

Pris en charge dans : 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Pris en charge dans : 4, 3.5 SP1

Pris en charge dans :

Windows 7, Windows Vista SP1 ou ultérieur, Windows XP SP3, Windows XP SP2 Édition x64, Windows Server 2008 (installation minimale non prise en charge), Windows Server 2008 R2 (installation minimale prise en charge avec SP1 ou version ultérieure), Windows Server 2003 SP2

Le .NET Framework ne prend pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.
Cela vous a-t-il été utile ?
(1500 caractères restants)
Contenu de la communauté Ajouter
Annotations FAQ