Export (0) Print
Expand All
Expand Minimize

GenericParameterAttributes Enumeration

Describes the constraints on a generic type parameter of a generic type or method.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

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

'Declaration
<FlagsAttribute> _
Public Enumeration GenericParameterAttributes

Member nameDescription
Supported by Silverlight for Windows PhoneSupported by Xbox 360NoneThere are no special flags.
Supported by Silverlight for Windows PhoneSupported by Xbox 360VarianceMaskSelects the combination of all variance flags. This value is the result of using logical OR to combine the following flags: Contravariant and Covariant.
Supported by Silverlight for Windows PhoneSupported by Xbox 360CovariantThe generic type parameter is covariant. A covariant type parameter can appear as the result type of a method, the type of a read-only field, a declared base type, or an implemented interface.
Supported by Silverlight for Windows PhoneSupported by Xbox 360ContravariantThe generic type parameter is contravariant. A contravariant type parameter can appear as a parameter type in method signatures.
Supported by Silverlight for Windows PhoneSupported by Xbox 360SpecialConstraintMaskSelects the combination of all special constraint flags. This value is the result of using logical OR to combine the following flags: DefaultConstructorConstraint, ReferenceTypeConstraint, and NotNullableValueTypeConstraint.
Supported by Silverlight for Windows PhoneSupported by Xbox 360ReferenceTypeConstraintA type can be substituted for the generic type parameter only if it is a reference type.
Supported by Silverlight for Windows PhoneSupported by Xbox 360NotNullableValueTypeConstraintA type can be substituted for the generic type parameter only if it is a value type and is not nullable.
Supported by Silverlight for Windows PhoneSupported by Xbox 360DefaultConstructorConstraintA type can be substituted for the generic type parameter only if it has a parameterless constructor.

The members of the GenericParameterAttributes enumeration are divided into two groups, the variance group and the special constraints group. To test a GenericParameterAttributes value for variance flags, first perform a bitwise AND operation with VarianceMask. If the result is None, there are no variance flags. Similarly, use SpecialConstraintMask to test for constraint flags.

The following code example defines a generic type Test with two type parameters. The second type parameter has a base class constraint and a reference type constraint. When the program executes, the constraints are examined using the Type.GenericParameterAttributes property and the Type.GetGenericParameterConstraints method.


Imports System.Reflection

' Define a sample interface to use as an interface constraint.
Public Interface ITest
End Interface

' Define a base type to use as a base class constraint.
Public Class Base
End Class

' 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(Of T As {Base, ITest}, U As {New, Class})
End Class

' Define a type that derives from Base and implements ITtest. This type
' satisfies the constraints on T in class Test.
Public Class Derived
   Inherits Base
   Implements ITest
End Class

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' To get the generic type definition, omit the type
      ' arguments but retain the comma to indicate the number
      ' of type arguments. 
      '
      Dim def As Type = GetType(Test(Of ,))
      outputBlock.Text += String.Format(vbCrLf & "Examining generic type {0}", def) & vbCrLf

      ' Get the type parameters of the generic type definition,
      ' and display them.
      '
      Dim defparams() As Type = def.GetGenericArguments()
      For Each tp As Type In defparams

         outputBlock.Text += String.Format(vbCrLf & "Type parameter: {0}", tp.Name) & vbCrLf
         outputBlock.Text &= vbTab & ListGenericParameterAttributes(tp) & vbCrLf

         ' List the base class and interface constraints. The
         ' constraints do not appear in any particular order. An
         ' empty array is returned if there are no constraints.
         '
         Dim tpConstraints As Type() = _
             tp.GetGenericParameterConstraints()
         For Each tpc As Type In tpConstraints
            outputBlock.Text &= vbTab & tpc.ToString() & vbCrLf
         Next tpc
      Next tp

   End Sub

   ' List the variance and special constraint flags.
   '
   Private Shared Function ListGenericParameterAttributes(ByVal t As Type) As String
      Dim retval As String
      Dim gpa As GenericParameterAttributes = t.GenericParameterAttributes

      ' Select the variance flags.
      Dim variance As GenericParameterAttributes = _
          gpa And GenericParameterAttributes.VarianceMask

      If variance = GenericParameterAttributes.None Then
         retval = "No variance flag;"
      Else
         If (variance And GenericParameterAttributes.Covariant) <> 0 Then
            retval = "Covariant;"
         Else
            retval = "Contravariant;"
         End If
      End If

      ' Select the constraint flags.
      Dim constraints As GenericParameterAttributes = _
          gpa And GenericParameterAttributes.SpecialConstraintMask

      If constraints = GenericParameterAttributes.None Then
         retval &= " no special constraints."
      Else
         If (constraints And GenericParameterAttributes.ReferenceTypeConstraint) <> 0 Then
            retval &= " ReferenceTypeConstraint"
         End If
         If (constraints And GenericParameterAttributes.NotNullableValueTypeConstraint) <> 0 Then
            retval &= " NotNullableValueTypeConstraint"
         End If
         If (constraints And GenericParameterAttributes.DefaultConstructorConstraint) <> 0 Then
            retval &= " DefaultConstructorConstraint"
         End If
      End If
      Return retval

   End Function
End Class
' 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
' 


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft