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.

適用於

另請參閱