Sdílet prostřednictvím


Type.IsGenericType Vlastnost

Definice

Získá hodnotu určující, zda aktuální typ je obecný typ.

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

Hodnota vlastnosti

trueje-li aktuálním typem obecný typ; v opačném případě . false

Příklady

Následující příklad kódu zobrazí hodnotu IsGenericType, , IsGenericTypeDefinitionIsGenericParametera ContainsGenericParameters vlastnosti pro typy popsané v oddílu Poznámky. Vysvětlení hodnot vlastností najdete v doprovodné tabulce v poznámkách.

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

Poznámky

IsGenericType Pomocí vlastnosti určete, zda Type objekt představuje obecný typ. ContainsGenericParameters Pomocí vlastnosti určete, zda Type objekt představuje otevřený vytvořený typ nebo uzavřený konstruovaný typ.

Poznámka:

Vlastnost IsGenericType vrátí false , pokud okamžitý typ není obecný. Například pole, jehož prvky jsou typu A<int> (A(Of Integer) v jazyce Visual Basic), není sám o sobě obecným typem.

Následující tabulka shrnuje neutrální podmínky pro běžné termíny používané v obecné reflexi.

Term Invariant
definice obecného typu Vlastnost IsGenericTypeDefinition je true.

Definuje obecný typ. Vytvořený typ je vytvořen voláním MakeGenericType metody na Type objektu, který představuje definici obecného typu a určení pole argumentů typu.

MakeGenericType lze volat pouze u definic obecného typu.

Definice obecného typu je obecný typ ( IsGenericType vlastnost je true), ale naopak není pravdivá.
obecný typ Vlastnost IsGenericType je true.

Může to být definice obecného typu, otevřený konstruovaný typ nebo uzavřený konstruovaný typ.

Všimněte si, že typ pole, jehož typ prvku je obecný, není sám o sobě obecný typ. Totéž platí Type pro objekt představující ukazatel na obecný typ.
open constructed type Vlastnost ContainsGenericParameters je true.

Příklady jsou obecný typ, který má nepřiřazené parametry typu, typ vnořený v definici obecného typu nebo otevřený konstruovaný typ nebo obecný typ, který má argument typu, pro který ContainsGenericParameters je truevlastnost .

Není možné vytvořit instanci otevřeného vytvořeného typu.

Všimněte si, že ne všechny otevřené konstruované typy jsou obecné. Například pole, jehož typ prvku je obecná definice typu obecný, a ukazatel na otevřený konstruovaný typ není obecný.
uzavřený konstruovaný typ Vlastnost ContainsGenericParameters je false.

Při rekurzivním zkoumání typ nemá žádné nepřiřazené obecné parametry.
parametr obecného typu Vlastnost IsGenericParameter je true.

Vlastnost ContainsGenericParameters je true.

V definici obecného typu je zástupný symbol pro typ, který bude přiřazen později.
argument obecného typu Může to být libovolný typ, včetně parametru obecného typu.

Argumenty typu jsou zadány jako pole Type objektů předávaných metodě MakeGenericType při vytváření vytvořeného obecného typu. Pokud se vytvoří instance výsledného typu, ContainsGenericParameters musí být false vlastnost určena pro všechny argumenty typu.

Následující příklad kódu a tabulka ilustrují některé z těchto termínů a invariantů. Třída Derived je obzvláště zajímavá, protože její základní typ je konstruovaný typ, který má kombinaci typů a parametrů typu v seznamu argumentů typu.

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

Následující tabulka ukazuje příklady, které používají a staví na třídách Base, Deriveda G. Pokud je kód C++ a C# stejný, zobrazí se jenom jedna položka.

Příklad Invariants
Derived(Of V)

Derived<V>
Pro tento typ:

IsGenericType je true.

IsGenericTypeDefinition je true.

ContainsGenericParameters je true.
Base(Of String, V)

Base<String,V>

Base<String^,V>
Pro tento typ:

IsGenericType je true.

IsGenericTypeDefinition je false.

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

Derived<int>[] d;

array<Derived<int>^>^ d;
Pro typ proměnné d:

IsGenericType je false to proto, že d je pole.

IsGenericTypeDefinition je false.

ContainsGenericParameters je false.
T, Ua V (všude, kde se zobrazují) IsGenericParameter je true.

IsGenericType protože false neexistuje způsob, jak omezit parametr typu na obecné typy.

IsGenericTypeDefinition je false.

ContainsGenericParameters je true to proto, že T, Ua V jsou samy o sobě parametry obecného typu. To neznamená nic o argumentech typu, které jsou jim přiřazeny později.
Typ pole F IsGenericType je true.

IsGenericTypeDefinitionje false to proto, že typ byl přiřazen k parametru typu .G Všimněte si, že to odpovídá tomu, že se tato metoda nazývá MakeGenericType .

ContainsGenericParameters je true to proto, že typ pole F má argument typu, který je otevřený konstruovaný typ. Vytvořený typ je otevřený, protože jeho argument typu (tj Base. ) je definice obecného typu. To znázorňuje rekurzivní povahu IsGenericType vlastnosti.
Vnořená třída Nested IsGenericType je true, i když Nested třída nemá vlastní parametry obecného typu, protože je vnořená do obecného typu.

IsGenericTypeDefinition je true. To znamená, že můžete volat metodu MakeGenericType a zadat parametr typu ohraničujícího typu , Derived.

ContainsGenericParameters je true to proto, Derivedže uzavřený typ má parametry obecného typu. To znázorňuje rekurzivní povahu ContainsGenericParameters vlastnosti.

Platí pro

Viz také