Type.GetGenericParameterConstraints Method

May 11, 2014

Returns an array of Type objects that represent the constraints on the current generic type parameter.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public virtual Type[] GetGenericParameterConstraints()

Return Value

Type: System.Type []
An array of Type objects that represent the constraints on the current generic type parameter.

ExceptionCondition
InvalidOperationException

The current Type object is not a generic type parameter. That is, the IsGenericParameter property returns false.

Each constraint on a generic type parameter is expressed as a Type object. Use the IsClass property to determine whether a constraint is the base class constraint; if the property returns false, the constraint is an interface constraint. If a type parameter has no class constraint and no interface constraints, an empty array is returned.

For a list of the invariant conditions for terms used in generic reflection, see the IsGenericType property remarks.

The following code example defines a generic type Test with two type parameters that have different constraints. When the program executes, the constraints are examined using the GenericParameterAttributes property and the GetGenericParameterConstraints method.


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 Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // 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<,>);
      outputBlock.Text += String.Format("\r\nExamining generic type {0}", def) + "\n";

      // Get the type parameters of the generic type definition,
      // and display them.
      //
      Type[] defparams = def.GetGenericArguments();
      foreach (Type tp in defparams)
      {
         outputBlock.Text += String.Format("\r\nType parameter: {0}", tp.Name) + "\n";
         outputBlock.Text += String.Format("\t{0}",
             ListGenericParameterAttributes(tp)) + "\n";

         // 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)
         {
            outputBlock.Text += String.Format("\t{0}", tpc) + "\n";
         }
      }
   }

   // 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 Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft