Type.GetGenericArguments 方法

定義

傳回 Type 物件的陣列,代表封閉泛型類型的類型引數,或泛型類型定義的類型參數。

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 物件組成的陣列,代表泛型類型的型別引數。 如果目前的類型不是泛型類型,則傳回空的陣列。

例外狀況

基底類別不支援叫用的方法。 衍生類別必須提供實作。

範例

下列程式碼範例會使用 GetGenericArguments 方法來顯示結構化類型的型別引數,以及其泛型型別定義的型別參數。

這個程式碼範例是針對屬性所提供之較大範例的一部分 IsGenericTypeDefinition 。 請參閱範例輸出的較大範例。

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

備註

陣列元素會以它們出現在泛型型別的型別引數清單中的順序傳回。

  • 如果目前的型別是封閉的結構化型別 (也就是 ContainsGenericParameters 屬性 false 傳回) ,則方法所傳回的陣列會 GetGenericArguments 包含已指派給泛型型別定義之泛型型別參數的類型。

  • 如果目前的型別是泛型型別定義,陣列就會包含型別參數。

  • 如果目前的型別是開放式的結構化型別 (也就是,屬性會傳回 ContainsGenericParameters true) ,其中特定類型尚未指派給封入泛型型別或方法的所有型別參數和型別參數,而陣列同時包含型別和型別參數。 您 IsGenericParameter 可以使用屬性來分辨它們。 如需此案例的示範,請參閱屬性的程式碼範例 ContainsGenericParameters

如需泛型反映中所使用之規範的恆成立條件清單,請參閱 IsGenericType 屬性備註。

適用於

另請參閱