Export (0) Print
Expand All
Expand Minimize

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)

'Declaration
Public Overridable Function GetGenericParameterConstraints As Type()

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.

Imports System
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 Main() 
        ' 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 ,))
        Console.WriteLine(vbCrLf & "Examining generic type {0}", def)

        ' 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

            Console.WriteLine(vbCrLf & "Type parameter: {0}", tp.Name)
            Console.WriteLine(vbTab & ListGenericParameterAttributes(tp))

            ' 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
                Console.WriteLine(vbTab & tpc.ToString())
            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 
' 

.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.

Show:
© 2014 Microsoft