Export (0) Print
Expand All
Expand Minimize

Type.GetGenericArguments Method

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

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

public virtual Type[] GetGenericArguments ()
public Type[] GetGenericArguments ()
public function GetGenericArguments () : Type[]
Not applicable.

Return Value

An array of Type objects that represent the type arguments of a generic type. Returns an empty array if the current type is not a generic type.

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

  • If the current type is a closed constructed type (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 type definition.

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

  • If the current type is an open constructed type (that is, the ContainsGenericParameters property returns true) in which specific types have not been assigned to all of the type parameters and type parameters of enclosing generic types or methods, 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 used in generic reflection, see the IsGenericType property remarks.

The following code example uses the GetGenericArguments method to display the type arguments of a constructed type and the type parameters of its generic type definition.

This code example is part of a larger example provided for the IsGenericTypeDefinition property.

if (t.IsGenericType)
{
    // If this is a generic type, display the type arguments.
    //
    Type[] typeArguments = t.GetGenericArguments();

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

    foreach (Type tParam in typeArguments)
    {
        // If this is a type parameter, display its
        // position.
        //
        if (tParam.IsGenericParameter)
        {
            Console.WriteLine("\t\t{0}\t(unassigned - parameter position {1})",
                tParam,
                tParam.GenericParameterPosition);
        }
        else
        {
            Console.WriteLine("\t\t{0}", tParam);
        }
    }
}

if (t.get_IsGenericType()) {
    // If this is a generic type, display the type arguments.
    Type typeArguments[] = t.GetGenericArguments();
    Console.WriteLine("\tList type arguments ({0}):",
        (Int32)typeArguments.get_Length());
    for (int iCtr = 0; iCtr < typeArguments.get_Length(); iCtr++) {
        Type tParam = typeArguments[iCtr];
        // If this is a type parameter, display its
        // position.
        
        if (tParam.get_IsGenericParameter()) {
            Console.WriteLine("\t\t{0}\t(unassigned - parameter "
                + "position {1})", tParam, 
                (Int32)tParam.get_GenericParameterPosition());
        }
        else {
            Console.WriteLine("\t\t{0}", tParam);
        }
    }
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft