Export (0) Print
Expand All

Type.GenericParameterPosition Property

Gets the position of the type parameter in the type parameter list of the generic type or method that declared the parameter, when the Type object represents a type parameter of a generic type or a generic method.

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

'Declaration
Public Overridable ReadOnly Property GenericParameterPosition As Integer

Property Value

Type: System.Int32
The position of a type parameter in the type parameter list of the generic type or method that defines the parameter. Position numbers begin at 0.

ExceptionCondition
InvalidOperationException

The current type does not represent a type parameter. That is, IsGenericParameter returns false.

The GenericParameterPosition property returns the position of a type parameter in the parameter list of the generic type definition or generic method definition where the type parameter was originally defined. The DeclaringType and DeclaringMethod properties identify the generic type or method definition:

  • If the DeclaringMethod property returns a MethodInfo, that MethodInfo represents a generic method definition, and the current Type object represents a type parameter of that generic method definition.

  • If the DeclaringMethod property returns Nothing, then the DeclaringType property always returns a Type object representing a generic type definition, and the current Type object represents a type parameter of that generic type definition.

To provide the correct context for the value of the GenericParameterPosition property, it is necessary to identify the generic type or method a type parameter belongs to. For example, consider the return value of the generic method GetSomething in the following code:

Public Class B(Of T, U)
End Class
Public Class A(Of V)
    Public Function GetSomething(Of X)() As B(Of V, X)
        Return New B(Of V, X)()
    End Function
End Class

The type returned by GetSomething depends on the type arguments supplied to class A and to GetSomething itself. You can obtain a MethodInfo for GetSomething, and from that you can obtain the return type. When you examine the type parameters of the return type, GenericParameterPosition returns 0 for both. The position of V is 0 because V is the first type parameter in the type parameter list for class A. The position of X is 0 because X is the first type parameter in the type parameter list for GetSomething.

NoteNote:

Calling the GenericParameterPosition property causes an exception if the current Type does not represent a type parameter. When you examine the type arguments of an open constructed type, use the IsGenericParameter property to tell which are type parameters and which are types. The IsGenericParameter property returns true for a type parameter; you can then use the GenericParameterPosition method to obtain its position and use the DeclaringMethod and DeclaringType properties to determine the generic method or type definition that defines it.

The following example defines a generic class with two type parameters and defines a second generic class that derives from the first class. The derived class's base class has two type arguments: the first is Int32, and the second is a type parameter of the derived type. The example displays information about these generic classes, including the positions reported by the GenericParameterPosition property.


Imports System.Reflection
Imports System.Collections.Generic

' Define a base class with two type parameters.
Public Class Base(Of T, U)
End Class

' Define a derived class. The derived class inherits from a constructed
' class that meets the following criteria:
'   (1) Its generic type definition is Base<T, U>.
'   (2) It uses int for the first type parameter.
'   (3) For the second type parameter, it uses the same type that is used
'       for the type parameter of the derived class.
' Thus, the derived class is a generic type with one type parameter, but
' its base class is an open constructed type with one assigned type
' parameter and one unassigned type parameter.
Public Class Derived(Of V)
   Inherits Base(Of Integer, V)
End Class

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text &= vbCrLf _
          & "--- Display a generic type and the open constructed" & vbCrLf
      outputBlock.Text &= "    type from which it is derived." & vbCrLf

      ' Create a Type object representing the generic type definition 
      ' for the Derived type, by omitting the type argument. (For
      ' types with multiple type parameters, supply the commas but
      ' omit the type arguments.) 
      '
      Dim derivedType As Type = GetType(Derived(Of ))
      DisplayGenericTypeInfo(outputBlock, derivedType)

      ' Display its open constructed base type.
      DisplayGenericTypeInfo(outputBlock, derivedType.BaseType)

   End Sub 'Main

   Private Shared Sub DisplayGenericTypeInfo(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal t As Type)
      outputBlock.Text &= String.Format(vbCrLf & "{0}", t) & vbCrLf

      outputBlock.Text &= vbTab & "Is this a generic type definition? " _
          & t.IsGenericTypeDefinition & vbCrLf

      outputBlock.Text &= vbTab & "Is it a generic type? " _
          & t.IsGenericType & vbCrLf

      outputBlock.Text &= vbTab _
          & "Does it have unassigned generic parameters? " _
          & t.ContainsGenericParameters & vbCrLf

      If t.IsGenericType Then
         ' If this is a generic type, display the type arguments.
         '
         Dim typeArguments As Type() = t.GetGenericArguments()

         outputBlock.Text &= vbTab & "List type arguments (" _
             & typeArguments.Length & "):" & vbCrLf

         For Each tParam As Type In typeArguments
            ' IsGenericParameter is true only for generic type
            ' parameters.
            '
            If tParam.IsGenericParameter Then
               outputBlock.Text &= vbTab & vbTab & tParam.ToString() & _
                   "  (unassigned - parameter position " _
                   & tParam.GenericParameterPosition & ")"
            Else
               outputBlock.Text &= vbTab & vbTab & tParam.ToString() & vbCrLf
            End If
         Next tParam
      End If

   End Sub 'DisplayGenericTypeInfo
End Class 'Test

' This example produces the following output:
'
'--- Display a generic type and the open constructed
'    type from which it is derived.
'
'Derived`1[V]
'        Is this a generic type definition? True
'        Is it a generic type? True
'        Does it have unassigned generic parameters? True
'        List type arguments (1):
'                V  (unassigned - parameter position 0)
'
'Base`2[System.Int32,V]
'        Is this a generic type definition? False
'        Is it a generic type? True
'        Does it have unassigned generic parameters? True
'        List type parameters (2):
'                System.Int32
'                V  (unassigned - parameter position 0)
' 


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