Sdílet prostřednictvím


Type.GetGenericArguments Metoda

Definice

Vrátí pole Type objektů, které představují argumenty typu uzavřeného obecného typu 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é představují argumenty typu obecného typu. Vrátí prázdnou matici, pokud aktuální typ není obecný typ.

Výjimky

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

Příklady

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

Tento příklad kódu je součástí většího příkladu zadaného IsGenericTypeDefinition pro vlastnost. Podívejte se na větší příklad ukázkového výstupu.

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.
    let typeArguments = t.GetGenericArguments()

    printfn $"\tList type arguments ({typeArguments.Length}):"

    for tParam in typeArguments do
        // If this is a type parameter, display its
        // position.
        if tParam.IsGenericParameter then
            printfn $"\t\t{tParam}\t(unassigned - parameter position {tParam.GenericParameterPosition})"
        else
            printfn $"\t\t{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 vráceny v pořadí, ve kterém se zobrazí v seznamu argumentů typu pro obecný typ.

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

  • Pokud je aktuálním typem definice obecného typu, pole obsahuje parametry typu.

  • Pokud je aktuálním typem otevřený konstruovaný typ (tj ContainsGenericParameters . vlastnost vrátí true), ve které nebyly přiřazeny ke všem parametrům typu a parametrům typu uzavřeným obecným typům nebo metodám, pole obsahuje oba typy a parametry typu. IsGenericParameter Pomocí vlastnosti je oddělte. Ukázku tohoto scénáře najdete v příkladu ContainsGenericParameters kódu pro vlastnost.

Seznam invariantních podmínek pro termíny použité v obecné reflexi najdete v IsGenericType poznámkách k vlastnostem.

Platí pro

Viz také