相关文章推荐
越狱的人字拖  ·  curl 模拟 GET\POST ...·  2 月前    · 
俊秀的海龟  ·  python 使用 Dis ...·  9 月前    · 
直爽的哑铃  ·  C# ...·  1 年前    · 
public:
 virtual property bool IsGenericType { bool get(); };
public virtual bool IsGenericType { get; }
member this.IsGenericType : bool
Public Overridable ReadOnly Property IsGenericType As Boolean

下面的代码示例显示备注部分中描述的类型的 、 IsGenericTypeDefinition IsGenericParameter ContainsGenericParameters 属性的值 IsGenericType 。 有关属性值的说明,请参阅备注中随附的表。

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

IsGenericType 使用 属性确定 对象是否 Type 表示泛型类型。 ContainsGenericParameters 使用 属性可确定 对象是表示开放构造类型还是 Type 封闭构造类型。

如果直接类型不是泛型,则 IsGenericType 属性返回 false 。 例如,元素在 Visual Basic) A(Of Integer) 中属于 (类型的 A<int> 数组本身不是泛型类型。

下表总结了泛型反射中使用的常用术语的固定条件。

Generic Type Definition — 泛型类型定义 IsGenericTypeDefinition 属性为 true

定义泛型类型。 构造类型是通过对 Type 表示泛型类型定义的 对象调用 MakeGenericType 方法并指定类型参数数组来创建的。

MakeGenericType 只能在泛型类型定义上调用。

任何泛型类型定义都是泛型类型, ( IsGenericType 属性 true ) ,但反之则不成立。 Generic Type — 泛型类型 IsGenericType 属性为 true

可以是泛型类型定义、开放构造类型或闭合构造类型。

请注意,其元素类型为泛型的数组类型本身不是泛型类型。 这同样适用于 Type 表示指向泛型类型的指针的 对象。 open 构造类型 ContainsGenericParameters 属性为 true

示例包括具有未分配类型参数的泛型类型、嵌套在泛型类型定义或开放构造类型中的类型,或具有属性为 的类型 ContainsGenericParameters 参数的 true 泛型类型。

无法创建开放式构造类型的实例。

请注意,并非所有开放式构造类型都是泛型类型。 例如,元素类型为泛型类型定义的数组不是泛型,指向打开的构造类型的指针不是泛型。 闭合构造类型 ContainsGenericParameters 属性为 false

以递归方式检查时,类型没有未分配的泛型参数。 Generic Type Parameter — 泛型类型参数 IsGenericParameter 属性为 true

ContainsGenericParameters 属性为 true

在泛型类型定义中,是稍后将分配的类型的占位符。 Generic Type Argument — 泛型类型变量 可以是任意类型,包括泛型类型参数。

创建构造泛 Type 型类型时,类型参数被指定为传递给 MakeGenericType 方法的对象数组。 如果要创建生成的类型的实例,则 ContainsGenericParameters 所有类型参数的 属性必须为 false

下面的代码示例和表演示了其中一些术语和固定项。 类 Derived 特别值得关注,因为它的基类型是一种构造类型,其类型参数列表中混合了类型和类型参数。

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

下表显示了使用 和 基于类 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 U V 本身就是泛型类型参数。 这并不表示有关稍后分配给它们的类型参数的任何内容。 字段的类型 F IsGenericType true

IsGenericTypeDefinition 是因为 false 类型已分配给 的类型 G 参数。 请注意,这等效于调用 MakeGenericType 方法。

ContainsGenericParameters 是因为 true 字段 F 的类型具有一个类型参数,该参数是一个打开的构造类型。 构造的类型是打开的,因为它的类型参数 (即 Base ,) 是泛型类型定义。 这说明了 属性的 IsGenericType 递归特性。 嵌套类 Nested IsGenericType true ,尽管 Nested 类没有自己的泛型类型参数,因为它嵌套在泛型类型中。

IsGenericTypeDefinition true 。 也就是说,可以调用 MakeGenericType 方法并提供封闭 Derived 类型 的类型参数。

ContainsGenericParameters 是因为 true 封闭类型 Derived 具有泛型类型参数。 这说明了 属性的 ContainsGenericParameters 递归特性。