Export (0) Print
Expand All

MethodInfo.GetGenericArguments Method

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)

[ComVisibleAttribute(true)]
public override Type[] GetGenericArguments()

Return Value

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

ExceptionCondition
NotSupportedException

This method is not supported.

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.

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)
{
    Type[] typeArguments = mi.GetGenericArguments();

    Console.WriteLine("\tList type arguments ({0}):", 
        typeArguments.Length);

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

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

Supported in: Windows 8

.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