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
|
Type.GetGenericParameterConstraints, méthode
Retourne un tableau d'objets Type qui représentent les contraintes qui s'exercent sur le paramètre de type générique actuel.
Assembly : mscorlib (dans mscorlib.dll)
| Exception | Condition |
|---|---|
| InvalidOperationException |
L'objet Type actuel n'est pas un paramètre de type générique. Ainsi, la propriété IsGenericParameter retourne la valeur false. |
Chaque contrainte qui s'exerce sur un paramètre de type générique est exprimée sous la forme d'un objet Type. Utilisez la propriété IsClass pour déterminer si une contrainte est la contrainte de classe de base ; si la propriété retourne false, la contrainte est une contrainte d'interface. Si un paramètre de type n'a aucune contrainte de classe ou d'interface, un tableau vide est retourné.
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 de code suivant définit un Test de type générique avec deux paramètres de type qui possèdent des contraintes différentes. Lorsque le programme s'exécute, les contraintes sont examinées à l'aide de la propriété GenericParameterAttributes et de la méthode GetGenericParameterConstraints.
using System; using System.Reflection; // Define a sample interface to use as an interface constraint. public interface ITest {} // Define a base type to use as a base class constraint. public class Base {} // Define the generic type to examine. The first generic type parameter, // T, derives from the class Base and implements ITest. This demonstrates // a base class constraint and an interface constraint. The second generic // type parameter, U, must be a reference type (class) and must have a // default constructor (new()). This demonstrates special constraints. // public class Test<T,U> where T : Base, ITest where U : class, new() {} // Define a type that derives from Base and implements ITest. This type // satisfies the constraints on T in class Test. public class Derived : Base, ITest {} public class Example { public static void Main() { // To get the generic type definition, omit the type // arguments but retain the comma to indicate the number // of type arguments. // Type def = typeof(Test<,>); Console.WriteLine("\r\nExamining generic type {0}", def); // Get the type parameters of the generic type definition, // and display them. // Type[] defparams = def.GetGenericArguments(); foreach (Type tp in defparams) { Console.WriteLine("\r\nType parameter: {0}", tp.Name); Console.WriteLine("\t{0}", ListGenericParameterAttributes(tp)); // List the base class and interface constraints. The // constraints are returned in no particular order. If // there are no class or interface constraints, an empty // array is returned. // Type[] tpConstraints = tp.GetGenericParameterConstraints(); foreach (Type tpc in tpConstraints) { Console.WriteLine("\t{0}", tpc); } } } // List the variance and special constraint flags. // private static string ListGenericParameterAttributes(Type t) { string retval; GenericParameterAttributes gpa = t.GenericParameterAttributes; GenericParameterAttributes variance = gpa & GenericParameterAttributes.VarianceMask; // Select the variance flags. if (variance == GenericParameterAttributes.None) retval = "No variance flag;"; else { if ((variance & GenericParameterAttributes.Covariant) != 0) retval = "Covariant;"; else retval = "Contravariant;"; } // Select GenericParameterAttributes constraints = gpa & GenericParameterAttributes.SpecialConstraintMask; if (constraints == GenericParameterAttributes.None) retval += " No special constraints"; else { if ((constraints & GenericParameterAttributes.ReferenceTypeConstraint) != 0) retval += " ReferenceTypeConstraint"; if ((constraints & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0) retval += " NotNullableValueTypeConstraint"; if ((constraints & GenericParameterAttributes.DefaultConstructorConstraint) != 0) retval += " DefaultConstructorConstraint"; } return retval; } } /* This example produces the following output: Examining generic type Test`2[T,U] Type parameter: T No variance flag; no special constraints. Base ITest Type parameter: U No variance flag; ReferenceTypeConstraint DefaultConstructorConstraint */
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.