Type.GetGenericArguments Metoda

Definice

Vrátí pole Type objektů, které reprezentují argumenty typu pro uzavřený obecný typ nebo parametry typu definice obecného typu.

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()

Návraty

Type[]

Pole Type objektů, které reprezentují argumenty typu obecného typu. Vrátí prázdné pole, pokud aktuální typ není obecný typ.

Výjimky

Vyvolaná metoda není v základní třídě podporována. Odvozené třídy musí poskytovat implementaci.

Příklady

Následující příklad kódu používá GetGenericArguments metodu k zobrazení argumentů typu konstruovaného typu a parametrů typu jeho definice obecného typu.

Tento příklad kódu je součástí většího příkladu, který je k dispozici pro IsGenericTypeDefinition vlastnost. Podívejte se na větší příklad výstupu s ukázkami.

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

Poznámky

Prvky pole jsou vraceny v pořadí, v jakém jsou uvedeny v seznamu argumentů typu pro obecný typ.

  • Pokud je aktuální typ uzavřený konstruovaný typ (to znamená, že se ContainsGenericParameters vrátí vlastnost false ), pole vrácené GetGenericArguments metodou obsahuje typy, které byly přiřazeny k parametrům obecného typu definice obecného typu.

  • Pokud je aktuální typ definicí obecného typu, pole obsahuje parametry typu.

  • Pokud je aktuální typ otevřený konstruovaný typ (to znamená, že se ContainsGenericParameters vrátí vlastnost true ), ve které konkrétní typy nebyly přiřazeny ke všem parametrům typu a parametry typu nadřazených obecných typů nebo metod, pole obsahuje oba typy i parametry typu. Pomocí IsGenericParameter vlastnosti je můžete říct od sebe. Ukázku tohoto scénáře naleznete v příkladu kódu pro ContainsGenericParameters vlastnost.

Seznam neutrálních podmínek pro výrazy používané v obecné reflexi naleznete v tématu IsGenericType Vlastnosti.

Platí pro

Viz také