Expand Minimize

Type::GenericParameterAttributes Property

Gets a combination of GenericParameterAttributes flags that describe the covariance and special constraints of the current generic type parameter.

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

public:
virtual property GenericParameterAttributes GenericParameterAttributes {
	GenericParameterAttributes get ();
}

Property Value

Type: System.Reflection::GenericParameterAttributes
A bitwise combination of GenericParameterAttributes values that describes the covariance and special constraints of the current generic type parameter.

ExceptionCondition
InvalidOperationException

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

NotSupportedException

The invoked method is not supported in the base class.

The value of this property contains flags that describe whether the current generic type parameter is covariant, and flags that describe any special constraints. Use the GenericParameterAttributes::VarianceMask value to select the covariance flags, and use the GenericParameterAttributes::SpecialConstraintMask value to select the constraint flags.

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
{
public:
   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 );
         }
      }
   }

private:

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

      // Select the variance flag.
      GenericParameterAttributes variance =
         static_cast<GenericParameterAttributes>(
            gpa & GenericParameterAttributes::VarianceMask );

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

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

      if ( constraints == GenericParameterAttributes::None )
            retval = String::Concat( retval, L" No special constraints" );
      else
      {
         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()
{
   Example::Main();
}

/* 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; No special constraints
 */

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft