Type.IsGenericType Type.IsGenericType Type.IsGenericType Type.IsGenericType Property

定义

获取一个值,该值指示当前类型是否是泛型类型。Gets a value indicating whether the current type is a generic type.

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

属性值

true如果当前类型是泛型类型,则为; 否则为。否则为falsetrue if the current type is a generic type; otherwise, false.

示例

下面的代码示例显示了 "备注" IsGenericType部分中IsGenericParameter描述的ContainsGenericParameters类型的、 IsGenericTypeDefinition、和属性的值。The following code example displays the value of the IsGenericType, IsGenericTypeDefinition, IsGenericParameter, and ContainsGenericParameters properties for the types described in the Remarks section. 有关属性值的说明,请参阅 "备注" 中附带的表。For explanations of the property values, see the accompanying table in Remarks.

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
 */
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

注解

使用属性来确定Type对象是否表示泛型类型。 IsGenericTypeUse the IsGenericType property to determine whether a Type object represents a generic type. 使用属性可确定Type对象表示开放构造类型还是封闭式构造类型。 ContainsGenericParametersUse the ContainsGenericParameters property to determine whether a Type object represents an open constructed type or a closed constructed type.

备注

如果IsGenericType直接类型false不是泛型,则属性返回。The IsGenericType property returns false if the immediate type is not generic. 例如,其元素属于类型A<int>A(Of Integer)在 Visual Basic 中)的数组本身不是泛型类型。For example, an array whose elements are of type A<int> (A(Of Integer) in Visual Basic) is not itself a generic type.

下表总结了泛型反射中使用的常用术语的固定条件。The following table summarizes the invariant conditions for common terms used in generic reflection.

术语Term 固定条件Invariant
Generic Type Definition — 泛型类型定义generic type definition IsGenericTypeDefinition 属性为 trueThe IsGenericTypeDefinition property is true.

定义泛型类型。Defines a generic type. 构造类型是通过MakeGenericType Type对表示泛型类型定义并指定类型参数数组的对象调用方法创建的。A constructed type is created by calling the MakeGenericType method on a Type object that represents a generic type definition and specifying an array of type arguments.

MakeGenericType只能对泛型类型定义调用。MakeGenericType can be called only on generic type definitions.

任何泛型类型定义都是泛型类型( IsGenericType属性为true),但反之不成立。Any generic type definition is a generic type (the IsGenericType property is true), but the converse is not true.
Generic Type — 泛型类型generic type IsGenericType 属性为 trueThe IsGenericType property is true.

可以是泛型类型定义、开放式构造类型或封闭式构造类型。Can be a generic type definition, an open constructed type, or a closed constructed type.

请注意,其元素类型为泛型的数组类型本身不是泛型类型。Note that an array type whose element type is generic is not itself a generic type. 表示指向泛型类型的指针Type的对象也是如此。The same is true of a Type object representing a pointer to a generic type.
开放式构造类型open constructed type ContainsGenericParameters 属性为 trueThe ContainsGenericParameters property is true.

例如,具有未分配的类型参数的泛型类型、嵌套在泛型类型定义中的类型或开放构造类型中的泛型类型,或者具有ContainsGenericParameters true属性所属的类型参数的泛型类型。Examples are a generic type that has unassigned type parameters, a type that is nested in a generic type definition or in an open constructed type, or a generic type that has a type argument for which the ContainsGenericParameters property is true.

不能创建开放式构造类型的实例。It is not possible to create an instance of an open constructed type.

请注意,并非所有开放式构造类型都是泛型的。Note that not all open constructed types are generic. 例如,其元素类型为泛型类型定义的数组不是泛型类型,而指向开放式构造类型的指针不是泛型类型。For example, an array whose element type is a generic type definition is not generic, and a pointer to an open constructed type is not generic.
封闭式构造类型closed constructed type ContainsGenericParameters 属性为 falseThe ContainsGenericParameters property is false.

递归检查时,类型没有未分配的泛型参数。When examined recursively, the type has no unassigned generic parameters.
Generic Type Parameter — 泛型类型参数generic type parameter IsGenericParameter 属性为 trueThe IsGenericParameter property is true.

ContainsGenericParameters 属性为 trueThe ContainsGenericParameters property is true.

在泛型类型定义中,将在稍后赋值的类型的占位符。In a generic type definition, a placeholder for a type that will be assigned later.
Generic Type Argument — 泛型类型参数generic type argument 可以是任何类型,包括泛型类型参数。Can be any type, including a generic type parameter.

类型参数指定为在创建构造的Type泛型类型时传递MakeGenericType给方法的对象数组。Type arguments are specified as an array of Type objects passed to the MakeGenericType method when creating a constructed generic type. 如果要创建结果类型的实例,则ContainsGenericParameters该属性false必须为所有类型自变量。If instances of the resulting type are to be created, the ContainsGenericParameters property must be false for all the type arguments.

下面的代码示例和表说明了其中一些术语和固定条件。The following code example and table illustrate some of these terms and invariants. Derived类特别感兴趣,因为其基类型是一个构造类型,该类型在其类型参数列表中混合了类型和类型参数。The Derived class is of particular interest because its base type is a constructed type that has a mixture of types and type parameters in its type argument list.

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> {}
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

下表显示在类BaseDerivedG上使用和生成的示例。The following table shows examples that use and build on the classes Base, Derived, and G. 当C++和C#代码相同时,只显示一个条目。When the C++ and C# code is the same, only one entry is shown.

示例Example 固定协定Invariants
Derived(Of V)

Derived<V>
对于此类型:For this type:

IsGenericTypetrueIsGenericType is true.

IsGenericTypeDefinitiontrueIsGenericTypeDefinition is true.

ContainsGenericParameterstrueContainsGenericParameters is true.
Base(Of String, V)

Base<String,V>

Base<String^,V>
对于此类型:For this type:

IsGenericTypetrueIsGenericType is true.

IsGenericTypeDefinitionfalseIsGenericTypeDefinition is false.

ContainsGenericParameterstrueContainsGenericParameters is true.
Dim d() As Derived(Of Integer)

Derived<int>[] d;

array<Derived<int>^>^ d;
对于变量d类型:For the type of variable d:

IsGenericTypefalsed因为是一个数组。IsGenericType is false because d is an array.

IsGenericTypeDefinitionfalseIsGenericTypeDefinition is false.

ContainsGenericParametersfalseContainsGenericParameters is false.
TUV (显示在任何位置)T, U, and V (everywhere they appear) IsGenericParametertrueIsGenericParameter is true.

IsGenericTypefalse因为没有办法将类型形参约束为泛型类型。IsGenericType is false because there is no way to constrain a type parameter to generic types.

IsGenericTypeDefinitionfalseIsGenericTypeDefinition is false.

ContainsGenericParameterstrue因为TU和是本身的泛型类型参数。VContainsGenericParameters is true because T, U, and V are themselves generic type parameters. 这并不意味着有关稍后分配给它们的类型参数的任何信息。This does not imply anything about type arguments that are assigned to them later.
字段的类型FThe type of field F IsGenericTypetrueIsGenericType is true.

IsGenericTypeDefinitionfalse因为类型已分配给的G类型参数。IsGenericTypeDefinition is false because a type has been assigned to the type parameter of G. 请注意,这等效于调用MakeGenericType方法。Note that this is equivalent to having called the MakeGenericType method.

ContainsGenericParameterstrue因为字段F的类型具有一个类型参数,该类型参数是一个开放式构造类型。ContainsGenericParameters is true because the type of field F has a type argument that is an open constructed type. 构造类型是开放式的,因为其类型参数( Base即)为泛型类型定义。The constructed type is open because its type argument (that is, Base) is a generic type definition. 这说明了IsGenericType属性的递归特性。This illustrates the recursive nature of the IsGenericType property.
嵌套类NestedThe nested class Nested IsGenericTypetrue,即使Nested类没有自己的泛型类型参数,因为它嵌套在泛型类型中。IsGenericType is true, even though the Nested class has no generic type parameters of its own, because it is nested in a generic type.

IsGenericTypeDefinitiontrueIsGenericTypeDefinition is true. 也就是说,您可以调用MakeGenericType方法并提供封闭Derived类型的类型参数。That is, you can call the MakeGenericType method and supply the type parameter of the enclosing type, Derived.

ContainsGenericParameterstrue因为封闭Derived类型具有泛型类型参数。ContainsGenericParameters is true because the enclosing type, Derived, has generic type parameters. 这说明了ContainsGenericParameters属性的递归特性。This illustrates the recursive nature of the ContainsGenericParameters property.

适用于

另请参阅