Lire en anglais

Partager via


Type.MakeGenericType(Type[]) Méthode

Définition

Substitue les éléments d'un tableau de types aux paramètres de type de la définition du type générique actuel et retourne un objet Type qui représente le type construit résultant.

C#
public abstract Type MakeGenericType(params Type[] typeArguments);
C#
public virtual Type MakeGenericType(params Type[] typeArguments);

Paramètres

typeArguments
Type[]

Tableau de types à remplacer pour les paramètres de type du type générique actuel.

Retours

Type représentant le type construit formé en substituant les éléments de typeArguments pour les paramètres de type du type générique actuel.

Exceptions

Le type actuel ne représente pas une définition de type générique. Autrement dit, IsGenericTypeDefinition retourne false.

typeArguments a la valeur null.

-ou-

Tout élément de typeArguments est null.

Le nombre d’éléments de typeArguments n’est pas le même que le nombre de paramètres de type de la définition de type générique actuelle.

- ou -

Un élément de typeArguments ne répond pas aux contraintes spécifiées pour le paramètre de type correspondant du type générique actuel.

- ou -

typeArguments contient un élément qui est un type pointeur (IsPointer retourne true), un type by-ref (IsByRef retourne true) ou Void.

La méthode appelée n’est pas prise en charge dans la classe de base. Les classes dérivées doivent fournir une implémentation.

Exemples

L’exemple suivant utilise la MakeGenericType méthode pour créer un type construit à partir de la définition de type générique pour le Dictionary<TKey,TValue> type. Le type construit représente un Dictionary<TKey,TValue> d’objets avec des clés de Test chaîne.

C#
using System;
using System.Reflection;
using System.Collections.Generic;

public class Test
{
    public static void Main()
    {
        Console.WriteLine("\r\n--- Create a constructed type from the generic Dictionary type.");

        // Create a type object representing the generic Dictionary 
        // type, by omitting the type arguments (but keeping the 
        // comma that separates them, so the compiler can infer the
        // number of type parameters).      
        Type generic = typeof(Dictionary<,>);
        DisplayTypeInfo(generic);

        // Create an array of types to substitute for the type
        // parameters of Dictionary. The key is of type string, and
        // the type to be contained in the Dictionary is Test.
        Type[] typeArgs = { typeof(string), typeof(Test) };

        // Create a Type object representing the constructed generic
        // type.
        Type constructed = generic.MakeGenericType(typeArgs);
        DisplayTypeInfo(constructed);

        // Compare the type objects obtained above to type objects
        // obtained using typeof() and GetGenericTypeDefinition().
        Console.WriteLine("\r\n--- Compare types obtained by different methods:");

        Type t = typeof(Dictionary<String, Test>);
        Console.WriteLine("\tAre the constructed types equal? {0}", t == constructed);
        Console.WriteLine("\tAre the generic types equal? {0}", 
            t.GetGenericTypeDefinition() == generic);
    }

    private static void DisplayTypeInfo(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);

        Type[] typeArguments = t.GetGenericArguments();
        Console.WriteLine("\tList type arguments ({0}):", typeArguments.Length);
        foreach (Type tParam in typeArguments)
        {
            Console.WriteLine("\t\t{0}", tParam);
        }
    }
}

/* This example produces the following output:

--- Create a constructed type from the generic Dictionary type.

System.Collections.Generic.Dictionary`2[TKey,TValue]
        Is this a generic type definition? True
        Is it a generic type? True
        List type arguments (2):
                TKey
                TValue

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

--- Compare types obtained by different methods:
        Are the constructed types equal? True
        Are the generic types equal? True
 */

Remarques

Pour plus d’informations sur cette API, consultez Remarques supplémentaires sur l’API pour Type.MakeGenericType.

S’applique à

Produit Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Voir aussi