Type.IsGenericParameter Egenskap

Definition

Hämtar ett värde som anger om den aktuella Type representerar en typparameter i definitionen av en allmän typ eller metod.

public:
 abstract property bool IsGenericParameter { bool get(); };
public:
 virtual property bool IsGenericParameter { bool get(); };
public abstract bool IsGenericParameter { get; }
public virtual bool IsGenericParameter { get; }
member this.IsGenericParameter : bool
Public MustOverride ReadOnly Property IsGenericParameter As Boolean
Public Overridable ReadOnly Property IsGenericParameter As Boolean

Egenskapsvärde

true om objektet Type representerar en typparameter av en allmän typdefinition eller allmän metoddefinition, falseannars .

Exempel

I följande exempel används IsGenericParameter egenskapen för att testa generiska typparametrar i en allmän typ.

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

Kommentarer

Type objekt som representerar generiska typparametrar kan hämtas genom att anropa GetGenericArguments metoden för ett Type objekt som representerar en allmän typdefinition eller GetGenericArguments metoden för ett MethodInfo objekt som representerar en allmän metoddefinition.

  • För en allmän typ eller metoddefinition IsGenericParameter returnerar true egenskapen för varje element i den resulterande matrisen.

  • För en sluten IsGenericParameter konstruktionstyp eller -metod returnerar false egenskapen för varje element i matrisen som returneras av GetGenericArguments metoden.

  • För en öppen konstruktionstyp eller -metod kan vissa element i matrisen vara specifika typer och andra kan vara typparametrar. IsGenericParameter returnerar false för typerna och true för typparametrarna. Kodexemplet för ContainsGenericParameters egenskapen visar en allmän klass med en blandning av typer och typparametrar.

En lista över de invarianta villkoren för termer som används i allmän reflektion finns i egenskapskommentarerna IsGenericType .

Gäller för

Se även