Type.IsGenericType Właściwość

Definicja

Pobiera wartość wskazującą, czy bieżący typ jest typem ogólnym.

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

Wartość właściwości

true jeśli bieżący typ jest typem ogólnym; w przeciwnym razie , false.

Przykłady

Poniższy przykład kodu przedstawia wartość IsGenericTypewłaściwości , IsGenericTypeDefinition, IsGenericParameteri ContainsGenericParameters dla typów opisanych w sekcji Uwagi. Aby uzyskać wyjaśnienia wartości właściwości, zobacz dołączącą tabelę w sekcji Uwagi.

using namespace System;
using namespace System::Reflection;

generic<typename T, typename U> public ref class Base {};

generic<typename T> public ref class G {};

generic<typename V> public ref class Derived : Base<String^, V>
{
public: 
    G<Derived<V>^>^ F;

    ref class Nested {};
};

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);
}

void main()
{
    // Get the generic type definition for Derived, and the base
    // type for Derived.
    //
    Type^ tDerived = Derived::typeid;
    Type^ tDerivedBase = tDerived->BaseType;

    // Declare an array of Derived<int>, and get its type.
    //
    array<Derived<int>^>^ d = gcnew array<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::typeid, as shown here,
    // or (2) specify a type parameter for Derived.
    //
    Type^ tT = Base::typeid->GetGenericArguments()[0];
    Type^ tF = tDerived->GetField("F")->FieldType;
    Type^ tNested = Derived::Nested::typeid;

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

/* This code example produces the following output:

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

Base type of generic<V> Derived
    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 generic<T> Base
    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 generic<V> Derived
    Type: Derived`1+Nested[V]
                    IsGenericType: True
          IsGenericTypeDefinition: True
        ContainsGenericParameters: True
               IsGenericParameter: False
 */
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

Uwagi

Użyj właściwości , IsGenericType aby określić, czy Type obiekt reprezentuje typ ogólny. Użyj właściwości , ContainsGenericParameters aby określić, czy Type obiekt reprezentuje otwarty typ skonstruowany, czy zamknięty typ skonstruowany.

Uwaga

Właściwość IsGenericType zwraca wartość false , jeśli typ natychmiastowy nie jest ogólny. Na przykład tablica, której elementy są typu A<int> (A(Of Integer) w Visual Basic) nie jest typem ogólnym.

Poniższa tabela zawiera podsumowanie niezmiennych warunków dla typowych terminów używanych w odbiciu ogólnym.

Okres Niezmienna
definicja typu ogólnego Właściwość IsGenericTypeDefinition to true.

Definiuje typ ogólny. Skonstruowany typ jest tworzony przez wywołanie MakeGenericType metody dla Type obiektu reprezentującego ogólną definicję typu i określenie tablicy argumentów typu.

MakeGenericType można wywoływać tylko w definicjach typów ogólnych.

Każda definicja typu ogólnego jest typem ogólnym ( IsGenericType właściwość to true), ale odwrotnie nie jest prawdziwe.
typ ogólny Właściwość IsGenericType to true.

Może być ogólną definicją typu, otwartym typem skonstruowanym lub zamkniętym typem skonstruowanym.

Należy pamiętać, że typ tablicy, którego typ elementu jest ogólny, nie jest typem ogólnym. To samo dotyczy Type obiektu reprezentującego wskaźnik do typu ogólnego.
otwarty typ skonstruowany Właściwość ContainsGenericParameters to true.

Przykłady to typ ogólny, który ma nieprzypisane parametry typu, typ zagnieżdżony w definicji typu ogólnego lub w otwartym skonstruowanym typie lub typ ogólny, który ma argument typu, dla którego ContainsGenericParameters właściwość jest true.

Nie można utworzyć wystąpienia typu otwartego skonstruowanego.

Należy pamiętać, że nie wszystkie otwarte typy skonstruowane są ogólne. Na przykład tablica, której typem elementu jest definicja typu ogólnego, nie jest ogólna, a wskaźnik do otwartego typu skonstruowanego nie jest ogólny.
zamknięty typ skonstruowany Właściwość ContainsGenericParameters to false.

Podczas badania cyklicznego typ nie ma nieprzypisanych parametrów ogólnych.
parametr typu ogólnego Właściwość IsGenericParameter to true.

Właściwość ContainsGenericParameters to true.

W definicji typu ogólnego symbol zastępczy typu, który zostanie przypisany później.
argument typu ogólnego Może być dowolnym typem, w tym parametrem typu ogólnego.

Argumenty typu są określane jako tablica Type obiektów przekazywanych do MakeGenericType metody podczas tworzenia skonstruowanego typu ogólnego. Jeśli należy utworzyć wystąpienia wynikowego typu, ContainsGenericParameters właściwość musi być false przeznaczona dla wszystkich argumentów typu.

Poniższy przykład kodu i tabela ilustrują niektóre z tych terminów i niezmiennych. Klasa Derived jest szczególnie interesująca, ponieważ jej typ podstawowy jest skonstruowanym typem, który ma kombinację typów i parametrów typu na liście argumentów typu.

generic<typename T, typename U> public ref class Base {};

generic<typename T> public ref class G {};

generic<typename V> public ref class Derived : Base<String^, V>
{
public:
    G<Derived<V>^>^ F;

    ref class Nested {};
};
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

W poniższej tabeli przedstawiono przykłady, które korzystają z klas Base, Derivedi G. Gdy kod C++ i C# jest taki sam, wyświetlany jest tylko jeden wpis.

Przykład Invariants
Derived(Of V)

Derived<V>
Dla tego typu:

IsGenericType to true.

IsGenericTypeDefinition to true.

ContainsGenericParameters to true.
Base(Of String, V)

Base<String,V>

Base<String^,V>
Dla tego typu:

IsGenericType to true.

IsGenericTypeDefinition to false.

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

Derived<int>[] d;

array<Derived<int>^>^ d;
Dla typu zmiennej d:

IsGenericType jest false to, ponieważ d jest tablicą.

IsGenericTypeDefinition to false.

ContainsGenericParameters to false.
T, Ui V (wszędzie, gdzie się pojawiają) IsGenericParameter to true.

IsGenericType Jest to false spowodowane tym, że nie ma możliwości ograniczenia parametru typu do typów ogólnych.

IsGenericTypeDefinition to false.

ContainsGenericParameters jest true to, ponieważ Tsame parametry typu , Ui V są samymi parametrami typu ogólnego. Nie oznacza to żadnych argumentów typu przypisanych do nich później.
Typ pola F IsGenericType to true.

IsGenericTypeDefinitionjest false spowodowane tym, że typ został przypisany do parametru typu .G Należy pamiętać, że jest to równoważne wywołaniu MakeGenericType metody .

ContainsGenericParameters jest true to, ponieważ typ pola F ma argument typu, który jest otwartym typem skonstruowanym. Skonstruowany typ jest otwarty, ponieważ jego argument typu (czyli Base) jest definicją typu ogólnego. Ilustruje to cykliczny charakter IsGenericType właściwości.
Zagnieżdżona klasa Nested IsGenericType to true, mimo że Nested klasa nie ma własnych parametrów typu ogólnego, ponieważ jest zagnieżdżona w typie ogólnym.

IsGenericTypeDefinition to true. Oznacza to, że można wywołać metodę MakeGenericType i podać parametr typu otaczającego typu . Derived

ContainsGenericParameters jest true to, ponieważ typ otaczający , Derivedma parametry typu ogólnego. Ilustruje to cykliczny charakter ContainsGenericParameters właściwości.

Dotyczy

Zobacz też