Condividi tramite


MethodInfo.GetGenericArguments Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns an array of Type objects that represent the type arguments of a generic method or the type parameters of a generic method definition.

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Overrides Function GetGenericArguments As Type()
[ComVisibleAttribute(true)]
public override Type[] GetGenericArguments()

Return Value

Type: array<System.Type[]
An array of Type objects that represent the type arguments of a generic method or the type parameters of a generic method definition. Returns an empty array if the current method is not a generic method.

Exceptions

Exception Condition
NotSupportedException

This method is not supported.

Remarks

The elements of the returned array are in the order in which they appear in the list of type parameters for the generic method.

  • If the current method is a closed constructed method (that is, the ContainsGenericParameters property returns false), the array returned by the GetGenericArguments method contains the types that have been assigned to the generic type parameters of the generic method definition.

  • If the current method is a generic method definition, the array contains the type parameters.

  • If the current method is an open constructed method (that is, the ContainsGenericParameters property returns true) in which specific types have been assigned to some type parameters and type parameters of enclosing generic types have been assigned to other type parameters, the array contains both types and type parameters. Use the IsGenericParameter property to tell them apart. For a demonstration of this scenario, see the code example for the ContainsGenericParameters property.

For a list of the invariant conditions for terms specific to generic methods, see the IsGenericMethod property. For a list of the invariant conditions for other terms used in generic reflection, see the Type.IsGenericType property.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 GetGenericArguments is present but not supported in Silverlight for Windows Phone.

Examples

The following code example shows how to get the type arguments of a generic method and display them.

This example is part of a larger example provided for the MakeGenericMethod method.

' If this is a generic method, display its type arguments.
'
If mi.IsGenericMethod Then
   Dim typeArguments As Type() = mi.GetGenericArguments()

   outputBlock.Text &= String.Format(vbTab & "List type arguments ({0}) & vbCrLf:", _
       typeArguments.Length)

   For Each tParam As Type In typeArguments
      ' IsGenericParameter is true only for generic type
      ' parameters.
      '
      If tParam.IsGenericParameter Then
         outputBlock.Text &= String.Format(vbTab & vbTab _
             & "{0}  parameter position: {1}" _
             & vbCrLf & vbTab & vbTab _
             & "   declaring method: {2}", _
             tParam, _
             tParam.GenericParameterPosition, _
             tParam.DeclaringMethod) & vbCrLf
      Else
         outputBlock.Text &= vbTab & vbTab & tParam.ToString() & vbCrLf
      End If
   Next tParam
End If
// If this is a generic method, display its type arguments.
//
if (mi.IsGenericMethod)
{
   Type[] typeArguments = mi.GetGenericArguments();

   outputBlock.Text += String.Format("\tList type arguments ({0}):",
       typeArguments.Length) + "\n";

   foreach (Type tParam in typeArguments)
   {
      // IsGenericParameter is true only for generic type
      // parameters.
      //
      if (tParam.IsGenericParameter)
      {
         outputBlock.Text += String.Format("\t\t{0}  parameter position {1}" +
             "\n\t\t   declaring method: {2}",
             tParam,
             tParam.GenericParameterPosition,
             tParam.DeclaringMethod) + "\n";
      }
      else
      {
         outputBlock.Text += String.Format("\t\t{0}", tParam) + "\n";
      }
   }
}

Version Information

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

Platforms

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