Nyelv

Type.IsGenericType Tulajdonság

Definíció

Beolvas egy értéket, amely jelzi, hogy az aktuális típus általános típus-e.

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

Tulajdonság értéke

trueha az aktuális típus általános típus; egyéb esetben. false

Példák

Az alábbi példakód a Megjegyzések szakaszban leírt típusok értékét IsGenericTypeIsGenericTypeDefinitionIsGenericParameterés ContainsGenericParameters tulajdonságait jeleníti meg. A tulajdonságértékek magyarázatát a megjegyzésekben található táblázat tartalmazza.

using System;
using System.Reflection;

public class Base<T, U> {}

public class Derived<V> : Base<string, V>
{
    public G<Derived <V>> F;

    public class Nested {}
}

public class G<T> {}

class Example
{
    public static void Main()
    {
        // Get the generic type definition for Derived, and the base
        // type for Derived.
        //
        Type tDerived = typeof(Derived<>);
        Type tDerivedBase = tDerived.BaseType;

        // Declare an array of Derived<int>, and get its type.
        //
        Derived<int>[] d = new Derived<int>[0];
        Type tDerivedArray = d.GetType();

        // Get a generic type parameter, the type of a field, and a
        // type that is nested in Derived. Notice that in order to
        // get the nested type it is necessary to either (1) specify
        // the generic type definition Derived<>, as shown here,
        // or (2) specify a type parameter for Derived.
        //
        Type tT = typeof(Base<,>).GetGenericArguments()[0];
        Type tF = tDerived.GetField("F").FieldType;
        Type tNested = typeof(Derived<>.Nested);

        DisplayGenericType(tDerived, "Derived<V>");
        DisplayGenericType(tDerivedBase, "Base type of Derived<V>");
        DisplayGenericType(tDerivedArray, "Array of Derived<int>");
        DisplayGenericType(tT, "Type parameter T from Base<T>");
        DisplayGenericType(tF, "Field type, G<Derived<V>>");
        DisplayGenericType(tNested, "Nested type in Derived<V>");
    }

    public static void DisplayGenericType(Type t, string caption)
    {
        Console.WriteLine("\n{0}", caption);
        Console.WriteLine("    Type: {0}", t);

        Console.WriteLine("\t            IsGenericType: {0}", 
            t.IsGenericType);
        Console.WriteLine("\t  IsGenericTypeDefinition: {0}", 
            t.IsGenericTypeDefinition);
        Console.WriteLine("\tContainsGenericParameters: {0}", 
            t.ContainsGenericParameters);
        Console.WriteLine("\t       IsGenericParameter: {0}", 
            t.IsGenericParameter);
    }
}

/* This code example produces the following output:

Derived<V>
    Type: Derived`1[V]
                    IsGenericType: True
          IsGenericTypeDefinition: True
        ContainsGenericParameters: True
               IsGenericParameter: False

Base type of Derived<V>
    Type: Base`2[System.String,V]
                    IsGenericType: True
          IsGenericTypeDefinition: False
        ContainsGenericParameters: True
               IsGenericParameter: False

Array of Derived<int>
    Type: Derived`1[System.Int32][]
                    IsGenericType: False
          IsGenericTypeDefinition: False
        ContainsGenericParameters: False
               IsGenericParameter: False

Type parameter T from Base<T>
    Type: T
                    IsGenericType: False
          IsGenericTypeDefinition: False
        ContainsGenericParameters: True
               IsGenericParameter: True

Field type, G<Derived<V>>
    Type: G`1[Derived`1[V]]
                    IsGenericType: True
          IsGenericTypeDefinition: False
        ContainsGenericParameters: True
               IsGenericParameter: False

Nested type in Derived<V>
    Type: Derived`1+Nested[V]
                    IsGenericType: True
          IsGenericTypeDefinition: True
        ContainsGenericParameters: True
               IsGenericParameter: False
 */
open System

type Base<'T, 'U>() = class end

type G<'T>() = class end

type Derived<'V>() =
    inherit Base<string, 'V>()
    
    [<DefaultValue>]
    val mutable public F : G<Derived<'V>>

let displayGenericType (t: Type) caption =
    printfn $"\n{caption}"
    printfn $"    Type: {t}"

    printfn $"\t            IsGenericType: {t.IsGenericType}" 
    printfn $"\t  IsGenericTypeDefinition: {t.IsGenericTypeDefinition}" 
    printfn $"\tContainsGenericParameters: {t.ContainsGenericParameters}"
    printfn $"\t       IsGenericParameter: {t.IsGenericParameter}"

// Get the generic type definition for Derived, and the base
// type for Derived.
let tDerived = typeof<Derived<_>>.GetGenericTypeDefinition()
let tDerivedBase = tDerived.BaseType

// Declare an array of Derived<int>, and get its type.
let d = Array.zeroCreate<Derived<int>> 0
let tDerivedArray = d.GetType()

// Get a generic type parameter, the type of a field, and a
// type that is nested in Derived. Notice that in order to
// get the nested type it is necessary to either (1) specify
// the generic type definition Derived<>, as shown here,
// or (2) specify a type parameter for Derived.
let tT = typeof<Base<_,_>>.GetGenericTypeDefinition().GetGenericArguments()[0]
let tF = tDerived.GetField("F").FieldType

displayGenericType tDerived "Derived<V>"
displayGenericType tDerivedBase "Base type of Derived<V>"
displayGenericType tDerivedArray "Array of Derived<int>"
displayGenericType tT "Type parameter T from Base<T>"
displayGenericType tF "Field type, G<Derived<V>>"

(* This code example produces the following output:

Derived<V>
    Type: Derived`1[V]
                    IsGenericType: True
          IsGenericTypeDefinition: True
        ContainsGenericParameters: True
               IsGenericParameter: False

Base type of Derived<V>
    Type: Base`2[System.String,V]
                    IsGenericType: True
          IsGenericTypeDefinition: False
        ContainsGenericParameters: True
               IsGenericParameter: False

Array of Derived<int>
    Type: Derived`1[System.Int32][]
                    IsGenericType: False
          IsGenericTypeDefinition: False
        ContainsGenericParameters: False
               IsGenericParameter: False

Type parameter T from Base<T>
    Type: T
                    IsGenericType: False
          IsGenericTypeDefinition: False
        ContainsGenericParameters: True
               IsGenericParameter: True

Field type, G<Derived<V>>
    Type: G`1[Derived`1[V]]
                    IsGenericType: True
          IsGenericTypeDefinition: False
        ContainsGenericParameters: True
               IsGenericParameter: False
 *)
Imports System.Reflection

' 
Public Class Base(Of T, U)
End Class

Public Class Derived(Of V) 
    Inherits Base(Of String, V)

    Public F As G(Of Derived(Of V))

    Public Class Nested
    End Class
End Class

Public Class G(Of T)
End Class 

Module Example

    Sub Main

        ' Get the generic type definition for Derived, and the base
        ' type for Derived.
        '
        Dim tDerived As Type = GetType(Derived(Of ))
        Dim tDerivedBase As Type = tDerived.BaseType

        ' Declare an array of Derived(Of Integer), and get its type.
        '
        Dim d(0) As Derived(Of Integer)
        Dim tDerivedArray As Type = d.GetType()

        ' Get a generic type parameter, the type of a field, and a
        ' type that is nested in Derived. Notice that in order to
        ' get the nested type it is necessary to either (1) specify
        ' the generic type definition Derived(Of ), as shown here,
        ' or (2) specify a type parameter for Derived.
        '
        Dim tT As Type = GetType(Base(Of ,)).GetGenericArguments()(0)
        Dim tF As Type = tDerived.GetField("F").FieldType
        Dim tNested As Type = GetType(Derived(Of ).Nested)

        DisplayGenericType(tDerived, "Derived(Of V)")
        DisplayGenericType(tDerivedBase, "Base type of Derived(Of V)")
        DisplayGenericType(tDerivedArray, "Array of Derived(Of Integer)")
        DisplayGenericType(tT, "Type parameter T from Base(Of T)")
        DisplayGenericType(tF, "Field type, G(Of Derived(Of V))")
        DisplayGenericType(tNested, "Nested type in Derived(Of V)")

    End Sub

    Sub DisplayGenericType(ByVal t As Type, ByVal caption As String)

        Console.WriteLine(vbLf & caption)
        Console.WriteLine("    Type: {0}", t)

        Console.WriteLine(vbTab & "            IsGenericType: {0}", _
            t.IsGenericType)
        Console.WriteLine(vbTab & "  IsGenericTypeDefinition: {0}", _
            t.IsGenericTypeDefinition)
        Console.WriteLine(vbTab & "ContainsGenericParameters: {0}", _
            t.ContainsGenericParameters)
        Console.WriteLine(vbTab & "       IsGenericParameter: {0}", _
            t.IsGenericParameter)

    End Sub

End Module

' This code example produces the following output:
'
'Derived(Of V)
'    Type: Derived`1[V]
'                    IsGenericType: True
'          IsGenericTypeDefinition: True
'        ContainsGenericParameters: True
'               IsGenericParameter: False
'
'Base type of Derived(Of V)
'    Type: Base`2[System.String,V]
'                    IsGenericType: True
'          IsGenericTypeDefinition: False
'        ContainsGenericParameters: True
'               IsGenericParameter: False
'
'Array of Derived(Of Integer)
'    Type: Derived`1[System.Int32][]
'                    IsGenericType: False
'          IsGenericTypeDefinition: False
'        ContainsGenericParameters: False
'               IsGenericParameter: False
'
'Type parameter T from Base(Of T)
'    Type: T
'                    IsGenericType: False
'          IsGenericTypeDefinition: False
'        ContainsGenericParameters: True
'               IsGenericParameter: True
'
'Field type, G(Of Derived(Of V))
'    Type: G`1[Derived`1[V]]
'                    IsGenericType: True
'          IsGenericTypeDefinition: False
'        ContainsGenericParameters: True
'               IsGenericParameter: False
'
'Nested type in Derived(Of V)
'    Type: Derived`1+Nested[V]
'                    IsGenericType: True
'          IsGenericTypeDefinition: True
'        ContainsGenericParameters: True
'               IsGenericParameter: False

Megjegyzések

IsGenericType A tulajdonság használatával meghatározhatja, hogy egy Type objektum egy általános típust jelöl-e. ContainsGenericParameters A tulajdonság használatával meghatározhatja, hogy egy Type objektum nyílt vagy zárt építésű típust jelöl-e.

Note

A IsGenericType tulajdonság akkor ad false vissza, ha az azonnali típus nem általános. Például egy olyan tömb, amelynek elemei A<int> típusúak (A(Of Integer) Visual Basic) nem általános típus.

Az alábbi táblázat az általános tükrözésben használt általános kifejezések invariáns feltételeit foglalja össze.

Term Invariáns
általános típus definíciója A IsGenericTypeDefinition tulajdonság true.

Általános típust határoz meg. A létrehozott típus úgy jön létre, hogy meghívja a MakeGenericType metódust egy Type általános típusdefiníciót képviselő objektumon, és megadja a típusargumentumok tömbét.

MakeGenericType csak általános típusdefiníciókon hívható meg.

Minden általános típusdefiníció egy általános típus (a IsGenericType tulajdonság), truede a converzum nem igaz.
általános típus A IsGenericType tulajdonság true.

Lehet általános típusdefiníció, nyitott, vagy zárt szerkezetű típus.

Vegye figyelembe, hogy egy tömbtípus, amelynek elemtípusa általános, nem általános típus. Ugyanez igaz egy Type általános típusra mutató mutatót ábrázoló objektumra is.
nyitott, konstruktált típus A ContainsGenericParameters tulajdonság true.

Ilyenek például az olyan általános típusok, amelyek nem hozzárendelt típusparaméterekkel, egy általános típusdefinícióba vagy egy nyitott, konstruktált típusba ágyazott típussal, vagy olyan általános típussal, amelynek típusargumentumával rendelkezik, amelyhez a ContainsGenericParameters tulajdonság tartozik true.

nem lehet létrehozni egy nyitott, konstruktált típusú példányt.

Vegye figyelembe, hogy nem minden nyitott, konstruktált típus általános. Például egy olyan tömb, amelynek elemtípusa általános típusdefiníció, nem általános, és a nyitott, konstruktált típusra mutató mutató nem általános.
zárt építésű típus A ContainsGenericParameters tulajdonság false.

Rekurzív vizsgálat esetén a típus nem rendelkezik hozzárendeletlen általános paraméterekkel.
általános típus paramétere A IsGenericParameter tulajdonság true.

A ContainsGenericParameters tulajdonság true.

Egy általános típusdefinícióban a később hozzárendelni kívánt típus helyőrzője.
általános típus argumentuma Bármilyen típus lehet, beleértve egy általános típusparamétert is.

A típusargumentumok a metódusnak Type átadott objektumok tömbjeként MakeGenericType vannak megadva egy létrehozott általános típus létrehozásakor. Ha létre kell hozni az eredményként kapott típuspéldányokat, a ContainsGenericParameters tulajdonságnak az összes típusargumentumhoz kell tartoznia false .

Az alábbi példakód és táblázat ezen kifejezések és invariánsok némelyikét szemlélteti. Az Derived osztály azért érdekes, mert alaptípusa egy olyan létrehozott típus, amely típus- és típusparaméterek keverékével rendelkezik a típus argumentumlistájában.

public class Base<T, U> {}

public class Derived<V> : Base<string, V>
{
    public G<Derived <V>> F;

    public class Nested {}
}

public class G<T> {}
type Base<'T, 'U>() = class end

type G<'T>() = class end

type Derived<'V>() =
    inherit Base<string, 'V>()
    
    [<DefaultValue>]
    val mutable public F : G<Derived<'V>>
Public Class Base(Of T, U)
End Class

Public Class Derived(Of V)
    Inherits Base(Of String, V)

    Public F As G(Of Derived(Of V))

    Public Class Nested
    End Class
End Class

Public Class G(Of T)
End Class

Az alábbi táblázat az osztályokra és azokra BaseDerivedGépülő példákat mutat be. Ha a C++ és a C# kód megegyezik, csak egy bejegyzés jelenik meg.

Example Invariánsok
Derived(Of V)

Derived<V>
Ehhez a típushoz:

IsGenericType az true.

IsGenericTypeDefinition az true.

ContainsGenericParameters az true.
Base(Of String, V)

Base<String,V>

Base<String^,V>
Ehhez a típushoz:

IsGenericType az true.

IsGenericTypeDefinition az false.

ContainsGenericParameters az true.
Dim d() As Derived(Of Integer)

Derived<int>[] d;

array<Derived<int>^>^ d;
A változó dtípusa:

IsGenericType azért van false , mert d egy tömb.

IsGenericTypeDefinition az false.

ContainsGenericParameters az false.
T, Ués V (mindenhol, ahol megjelennek) IsGenericParameter az true.

IsGenericType ennek az az oka false , hogy a típusparamétert nem lehet általános típusokra korlátozni.

IsGenericTypeDefinition az false.

ContainsGenericParameters azért van, true mert T, Ués V maguk is általános típusparaméterek. Ez nem utal a később hozzájuk rendelt típusargumentumokra.
A mező típusa F IsGenericType az true.

IsGenericTypeDefinition annak az oka, false hogy egy típus hozzá lett rendelve a típusparaméterhez G. Vegye figyelembe, hogy ez egyenértékű a metódus meghívásával MakeGenericType .

ContainsGenericParameters azért van, true mert a mezőtípusnak F van egy olyan típusargumentuma, amely egy nyitott, konstruktált típus. A létrehozott típus nyitott, mert a típusargumentum (azaz Base) egy általános típusdefiníció. Ez a tulajdonság rekurzív jellegét IsGenericType mutatja be.
A beágyazott osztály Nested IsGenericType annak trueellenére, hogy az Nested osztály nem rendelkezik saját általános típusparaméterekkel, mivel általános típusba van ágyazva.

IsGenericTypeDefinition az true. Ez azt jelzi, hogy meghívhatja a MakeGenericType metódust, és megadhatja a beágyazási típus típusparaméterét. Derived

ContainsGenericParameters annak az oka true , Derivedhogy a beágyazási típus általános típusparaméterekkel rendelkezik. Ez a tulajdonság rekurzív jellegét ContainsGenericParameters mutatja be.

A következőre érvényes:

Lásd még