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 プロパティの解説を参照してください。

適用対象

こちらもご覧ください