GetGenericParameterConstraints Method
Collapse the table of content
Expand the table of content

Type::GetGenericParameterConstraints Method ()


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

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

virtual array<Type^>^ GetGenericParameterConstraints()

Return Value

Type: array<System::Type^>^

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

Exception Condition

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 namespace System;
using namespace System::Collections;
using namespace System::Reflection;

// Define a sample interface to use as an interface constraint.
interface class ITest{};

// Define a base type to use as a class constraint.
public ref 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. In the .NET
// Framework version 2.0, C++ has no way of expressing special constraints.
// See the C# example code.
generic <typename T, typename U>
   where T :  Base, ITest
ref class Test {};

// Define a type that derives from Base and implements interface
// ITest. This type satisfies the constraint on T in class Test.
public ref class Derived: public Base, public ITest {};

public ref class Example
   static void Main()
      // Create a constructed type from Test<T,U>, and from it
      // get the generic type definition.
      Type^ def = Test::typeid;
      Console::WriteLine( L"\r\nExamining generic type {0}", def );

      // Get the type parameters of the generic type definition,
      // and display them.
      for each (Type^ tp in def->GetGenericArguments())
         Console::WriteLine( L"\r\nType parameter: {0}", tp);
         Console::WriteLine( L"\t{0}", 
            ListGenericParameterAttributes( tp ) );

         // List the base class and interface constraints. The
         // constraints do not appear in any particular order. If
         // there are no class or interface constraints, an empty
         // array is returned.
         for each (Type^ constraint in tp->GetGenericParameterConstraints())
            Console::WriteLine( L"\t{0}", constraint );


   // List the variance and special constraint flags. 
   static String^ ListGenericParameterAttributes( Type^ t )
      String^ retval;
      GenericParameterAttributes gpa = t->GenericParameterAttributes;

      // Select the variance flag.
      GenericParameterAttributes variance =
            gpa & GenericParameterAttributes::VarianceMask );

      if ( variance == GenericParameterAttributes::None )
            retval = L"No variance flag;";
         if ( (variance & GenericParameterAttributes::Covariant) !=
               GenericParameterAttributes::None )
            retval = L"Covariant;";
            retval = L"Contravariant;";

      // Select the special constraint flags.
      GenericParameterAttributes constraints =
            gpa & GenericParameterAttributes::SpecialConstraintMask);

      if ( constraints == GenericParameterAttributes::None )
            retval = String::Concat( retval, L" No special constraints" );
         if ( (constraints & GenericParameterAttributes::ReferenceTypeConstraint) !=
               GenericParameterAttributes::None )
            retval = String::Concat( retval, L" ReferenceTypeConstraint" );

         if ( (constraints & GenericParameterAttributes::NotNullableValueTypeConstraint) !=
               GenericParameterAttributes::None )
            retval = String::Concat( retval, L" NotNullableValueTypeConstraint" );

         if ( (constraints & GenericParameterAttributes::DefaultConstructorConstraint) !=
               GenericParameterAttributes::None )
            retval = String::Concat( retval, L" DefaultConstructorConstraint" );

      return retval;

int main()

/* This example produces the following output:

Examining generic type Test`2[T,U]

Type parameter: T
        No variance flag; No special constraints

Type parameter: U
        No variance flag; No special constraints

.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
© 2015 Microsoft