Поделиться через


Type.IsGenericType Свойство

Определение

Возвращает значение, указывающее, является ли текущий тип универсальным типом.

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

Значение свойства

true Значение , если текущий тип является универсальным; falseв противном случае .

Примеры

В следующем примере кода отображается значение IsGenericTypeтипа , IsGenericTypeDefinitionIsGenericParameterи ContainsGenericParameters свойств для типов, описанных в разделе "Примечания". Описание значений свойств см. в следующей таблице в примечаниях.

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

Комментарии

IsGenericType Используйте свойство, чтобы определить, представляет ли Type объект универсальный тип. ContainsGenericParameters Используйте свойство, чтобы определить, представляет ли Type объект открытый созданный тип или закрытый построенный тип.

Замечание

Свойство возвращаетсяfalse, IsGenericType если непосредственный тип не является универсальным. Например, массив, элементы которого имеют тип A<int> (A(Of Integer) в Visual Basic) не является универсальным типом.

В следующей таблице перечислены инвариантные условия для распространенных терминов, используемых в универсальном отражении.

Срок Инвариант
определение универсального типа Свойство IsGenericTypeDefinition является true.

Определяет универсальный тип. Созданный тип создается путем вызова MakeGenericType метода в Type объекте, представляющего определение универсального типа и указывая массив аргументов типа.

MakeGenericType можно вызывать только в определениях универсальных типов.

Любое определение универсального типа является универсальным типом ( IsGenericType свойством является true), но наоборот не верно.
универсальный тип Свойство IsGenericType является true.

Может быть определение универсального типа, открытый созданный тип или закрытый созданный тип.

Обратите внимание, что тип массива, тип элемента которого является универсальным, не является универсальным. То же самое относится к Type объекту, представляющего указатель на универсальный тип.
открытый созданный тип Свойство ContainsGenericParameters является true.

Примерами являются универсальный тип, имеющий параметры неназначенных типов, тип, вложенный в определение универсального типа или открытый построенный тип, или универсальный тип, имеющий аргумент типа, для которого ContainsGenericParameters это trueсвойство.

Невозможно создать экземпляр открытого созданного типа.

Обратите внимание, что не все открытые созданные типы являются универсальными. Например, массив, тип элемента которого является определением универсального типа, не является универсальным, а указатель на открытый созданный тип не является универсальным.
закрытый созданный тип Свойство ContainsGenericParameters является false.

При рекурсивном анализе тип не имеет неназначенных универсальных параметров.
параметр универсального типа Свойство IsGenericParameter является true.

Свойство ContainsGenericParameters является true.

В определении универсального типа заполнитель для типа, который будет назначен позже.
аргумент универсального типа Может быть любым типом, включая параметр универсального типа.

Аргументы типа указываются в виде массива объектов, передаваемых TypeMakeGenericType методу при создании созданного универсального типа. Если необходимо создать экземпляры результирующего типа, ContainsGenericParameters свойство должно быть false для всех аргументов типа.

В следующем примере кода и таблице показаны некоторые из этих терминов и инвариантных. Класс Derived имеет особый интерес, так как его базовый тип является созданным типом, который имеет смесь типов и параметров типа в списке аргументов типа.

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

В следующей таблице показаны примеры, которые используют и строят классы Base, Derivedи G. Если код C++ и C# совпадает, отображается только одна запись.

Пример Инварианты
Derived(Of V)

Derived<V>
Для этого типа:

IsGenericType равно true.

IsGenericTypeDefinition равно true.

ContainsGenericParameters равно true.
Base(Of String, V)

Base<String,V>

Base<String^,V>
Для этого типа:

IsGenericType равно true.

IsGenericTypeDefinition равно false.

ContainsGenericParameters равно true.
Dim d() As Derived(Of Integer)

Derived<int>[] d;

array<Derived<int>^>^ d;
Для типа переменной d:

IsGenericType является false массивом d .

IsGenericTypeDefinition равно false.

ContainsGenericParameters равно false.
T, Uи V (везде, где они появляются) IsGenericParameter равно true.

IsGenericType это false связано с отсутствием способа ограничить параметр типа универсальными типами.

IsGenericTypeDefinition равно false.

ContainsGenericParametersэто true связано с тем, что Tи UV сами являются параметрами универсального типа. Это не означает ничего о аргументах типа, назначенных им позже.
Тип поля F IsGenericType равно true.

IsGenericTypeDefinitionfalse это связано с тем, что тип был назначен параметру Gтипа . Обратите внимание, что это эквивалентно вызову MakeGenericType метода.

ContainsGenericParameters это true связано с тем, что тип поля F имеет аргумент типа, который является открытым созданным типом. Созданный тип открыт, так как его аргумент типа (т Base. е. ) является определением универсального типа. Это иллюстрирует рекурсивный характер IsGenericType свойства.
Вложенный класс Nested IsGenericType имеется true, хотя Nested класс не имеет собственных параметров универсального типа, так как он вложен в универсальный тип.

IsGenericTypeDefinition равно true. То есть можно вызвать MakeGenericType метод и указать параметр типа включаемого типа. Derived

ContainsGenericParameters это true связано с тем, что заключенный тип имеет Derivedпараметры универсального типа. Это иллюстрирует рекурсивный характер ContainsGenericParameters свойства.

Применяется к

См. также раздел