相关文章推荐
聪明的啤酒  ·  [Solved] ...·  2 月前    · 
焦虑的领结  ·  为什么用 let ...·  1 年前    · 
精明的开心果  ·  FFMPEG Tips (3) ...·  1 年前    · 
public:
 virtual property bool IsGenericType { bool get(); };
public virtual bool IsGenericType { get; }
member this.IsGenericType : bool
Public Overridable ReadOnly Property IsGenericType As Boolean

下列程式碼範例會顯示一節中所述之型別的 IsGenericType IsGenericTypeDefinition IsGenericParameter 、 和 ContainsGenericParameters 屬性的值。 如需屬性值的說明,請參閱中的隨附資料表。

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<int> ( A(Of Integer) 的陣列不是泛型型別。

下表摘要說明泛型反映中常用詞彙的不變異條件。

Generic Type Definition - 泛型類型定義 IsGenericTypeDefinition 屬性為 true

定義泛型型別。 建構型別是藉由呼叫 MakeGenericType 物件上的 Type 方法來建立,這個物件代表泛型型別定義,並指定型別引數的陣列。

MakeGenericType 只能在泛型型別定義上呼叫。

任何泛型型別定義都是泛型型別, ( IsGenericType 屬性是 true ) ,但相反的不是 true。 Generic Type - 泛型類型 IsGenericType 屬性為 true

可以是泛型型別定義、開放式建構型別或封閉式建構型別。

請注意,其專案類型為泛型的陣列類型不是泛型型別。 表示 Type 泛型型別指標的物件也是如此。 open 建構類型 ContainsGenericParameters 屬性為 true

範例是具有未指派型別參數的泛型型別、泛型型別定義或開放式建構型別中的巢狀類型,或是具有屬性為 true 之型別引數的 ContainsGenericParameters 泛型型別。

您無法建立開放式建構型別的實例。

請注意,並非所有開啟的建構型別都是泛型。 例如,元素類型為泛型型別定義的陣列不是泛型,而開啟建構型別的指標則不是泛型。 封閉式建構類型 ContainsGenericParameters 屬性為 false

以遞迴方式檢查時,類型沒有未指派的泛型參數。 Generic Type Parameter - 泛型型別參數 IsGenericParameter 屬性為 true

ContainsGenericParameters 屬性為 true

在泛型型別定義中,將會在稍後指派的型別預留位置。 Generic Type Argument - 泛型類型引數 可以是任何類型,包括泛型型別參數。

建立建構的泛型型別時,型別引數會指定為傳遞至 MakeGenericType 方法的物件 Type 陣列。 如果要建立所產生型別的實例, 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

下表顯示使用 和 建置在 、 Derived G 類別 Base 上的範例。 當 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 遞迴本質。