Type.GetGenericArguments Type.GetGenericArguments Type.GetGenericArguments Type.GetGenericArguments Method

定义

返回表示封闭式泛型类型的类型参数或泛型类型定义的类型参数的 Type 对象的数组。Returns an array of Type objects that represent the type arguments of a closed generic type or the type parameters of a generic type definition.

public:
 virtual cli::array <Type ^> ^ GetGenericArguments();
public virtual Type[] GetGenericArguments ();
abstract member GetGenericArguments : unit -> Type[]
override this.GetGenericArguments : unit -> Type[]
Public Overridable Function GetGenericArguments () As Type()

返回

Type[]

表示泛型类型的类型实参的 Type 对象的数组。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 invoked method is not supported in the base class. 派生类必须提供一个实现。Derived classes must provide an implementation.

示例

下面的代码示例使用GetGenericArguments方法来显示构造的类型的类型参数和其泛型类型定义的类型参数。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.

此代码示例是为提供一个更大示例的一部分IsGenericTypeDefinition属性。This code example is part of a larger example provided for the IsGenericTypeDefinition property. 请参阅示例输出的较大示例。See the larger example for sample output.

if ( t->IsGenericType )
{
   
   // If this is a generic type, display the type arguments.
   //
   array<Type^>^typeArguments = t->GetGenericArguments();
   Console::WriteLine( L"\tList type arguments ({0}):",
      typeArguments->Length );
   System::Collections::IEnumerator^ myEnum =
      typeArguments->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Type^ tParam = safe_cast<Type^>(myEnum->Current);
      
      // If this is a type parameter, display its
      // position.
      //
      if ( tParam->IsGenericParameter )
      {
         Console::WriteLine(
            L"\t\t{0}\t(unassigned - parameter position {1})",
            tParam, tParam->GenericParameterPosition );
      }
      else
      {
         Console::WriteLine( L"\t\t{0}", tParam );
      }
   }
}
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.IsGenericType Then
    ' If this is a generic type, display the type arguments.
    '
    Dim typeArguments As Type() = t.GetGenericArguments()
    
    Console.WriteLine(vbTab & "List type arguments (" _
        & typeArguments.Length & "):")
    
    For Each tParam As Type In typeArguments
        ' If this is a type parameter, display its position.
        '
        If tParam.IsGenericParameter Then
            Console.WriteLine(vbTab & vbTab & tParam.ToString() _
                & vbTab & "(unassigned - parameter position " _
                & tParam.GenericParameterPosition & ")")
        Else
            Console.WriteLine(vbTab & vbTab & tParam.ToString())
        End If
    Next tParam
End If

注解

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

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

  • 如果当前类型是开放式构造的类型 (即ContainsGenericParameters属性返回true) 中的特定类型尚未分配到的所有类型参数和类型参数的封闭式泛型类型或方法,该数组包含类型和类型参数。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. 使用IsGenericParameter属性来区分它们。Use the IsGenericParameter property to tell them apart. 此方案的演示,请参阅的代码示例ContainsGenericParameters属性。For a demonstration of this scenario, see the code example for the ContainsGenericParameters property.

有关泛型反射中使用的术语的固定条件列表,请参阅 IsGenericType 属性注解。For a list of the invariant conditions for terms used in generic reflection, see the IsGenericType property remarks.

适用于

另请参阅