Type.IsGenericType 屬性

定義

取得值,指出目前類型是否為泛型類型。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

屬性值

Boolean

true 如果目前的類型是泛型型別,則為,否則為 falsetrue if the current type is a generic type; otherwise, false.

範例

下列程式碼範例會顯示 [備註] IsGenericType IsGenericTypeDefinition IsGenericParameter ContainsGenericParameters 區段中所述之類型的、、和屬性的值。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

備註

IsGenericType 可以使用屬性來判斷 Type 物件是否代表泛型型別。Use the IsGenericType property to determine whether a Type object represents a generic type. ContainsGenericParameters 可以使用屬性來判斷 Type 物件是否代表開啟的結構化型別或封閉的結構化型別。Use 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. 例如,在 Visual Basic) 中,其專案類型 A<int> (的陣列 A(Of Integer) 本身不是泛型型別。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. 建立的型別是藉由 MakeGenericTypeType 代表泛型型別定義的物件上呼叫方法,並指定型別引數的陣列來建立。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) 的,但反向的則不是 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 trueExamples 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 傳遞給方法的物件陣列 MakeGenericTypeType arguments are specified as an array of Type objects passed to the MakeGenericType method when creating a constructed generic type. 如果要建立產生之型別的實例,則 ContainsGenericParameters 所有型別引數的屬性都必須是 falseIf 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

下表顯示在類別、和上使用和建立的 Base 範例 Derived GThe 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;
針對變數的類型 dFor the type of variable d:

IsGenericTypefalse 因為 d 是陣列。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 因為 TUV 本身都是泛型型別參數。ContainsGenericParameters 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 因為類型已指派給的型別參數 GIsGenericTypeDefinition 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.

ContainsGenericParameters 這是 true 因為欄位的型別 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. 這會說明屬性的遞迴性質 IsGenericTypeThis 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 並提供封入類型的型別參數 DerivedThat 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. 這會說明屬性的遞迴性質 ContainsGenericParametersThis illustrates the recursive nature of the ContainsGenericParameters property.

適用於

另請參閱