MethodInfo.GetGenericArguments 方法

定义

返回 Type 对象的数组,这些对象表示泛型方法的类型实参或泛型方法定义的类型形参。Returns an array of Type objects that represent the type arguments of a generic method or the type parameters of a generic method definition.

public:
 override cli::array <Type ^> ^ GetGenericArguments();
public override Type[] GetGenericArguments ();
[System.Runtime.InteropServices.ComVisible(true)]
public override Type[] GetGenericArguments ();
override this.GetGenericArguments : unit -> Type[]
[<System.Runtime.InteropServices.ComVisible(true)>]
override this.GetGenericArguments : unit -> Type[]
Public Overrides Function GetGenericArguments () As Type()

返回

Type[]

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.

属性

例外

不支持此方法。This method is not supported.

示例

下面的代码示例演示如何获取泛型方法的类型实参并显示它们。The following code example shows how to get the type arguments of a generic method and display them.

此示例摘自为方法提供的一个更大的示例 MakeGenericMethodThis 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)
{
    array<Type^>^ typeArguments = mi->GetGenericArguments();

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

    for each (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);
        }
    }
}
// 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);
        }
    }
}
' If this is a generic method, display its type arguments.
'
If mi.IsGenericMethod Then
    Dim typeArguments As Type() = mi.GetGenericArguments()
    
    Console.WriteLine(vbTab & "List type arguments ({0}):", _
        typeArguments.Length)
    
    For Each tParam As Type In typeArguments
        ' IsGenericParameter is true only for generic type
        ' parameters.
        '
        If tParam.IsGenericParameter Then
            Console.WriteLine(vbTab & vbTab _
                & "{0}  parameter position: {1}" _
                & vbCrLf & vbTab & vbTab _
                & "   declaring method: {2}", _
                tParam,  _
                tParam.GenericParameterPosition, _
                tParam.DeclaringMethod)
        Else
            Console.WriteLine(vbTab & vbTab & tParam.ToString())
        End If
    Next tParam
End If

注解

返回数组的元素按它们出现在泛型方法的类型参数列表中的顺序排列。The elements of the returned array are in the order in which they appear in the list of type parameters for the generic method.

  • 如果当前方法是封闭式构造方法 (即, ContainsGenericParameters 属性返回 false) ,则方法返回的数组 GetGenericArguments 包含已分配给泛型方法定义的泛型类型参数的类型。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.

  • 如果当前方法是开放构造方法 (即,则 ContainsGenericParameters 属性将返回 true) 在此方法中,已将特定类型分配给某些类型参数,并已将封闭泛型类型的类型参数分配给其他类型参数,数组同时包含类型参数和类型参数。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. 使用 IsGenericParameter 属性将它们区分开来。Use the IsGenericParameter property to tell them apart. 有关此方案的演示,请参阅属性的代码示例 ContainsGenericParametersFor a demonstration of this scenario, see the code example for the ContainsGenericParameters property.

有关特定于泛型方法的术语的固定条件列表,请参见 IsGenericMethod 属性。For a list of the invariant conditions for terms specific to generic methods, see the IsGenericMethod property. 有关泛型反射中使用的其他术语的固定条件列表,请参见 Type.IsGenericType 属性。For a list of the invariant conditions for other terms used in generic reflection, see the Type.IsGenericType property.

适用于

另请参阅