Type.IsGenericTypeDefinition 屬性

定義

取得值,指出目前的 Type 是否表示可用於建構其他泛型類型的泛型類型定義。

C#
public virtual bool IsGenericTypeDefinition { get; }

屬性值

如果 Type 物件表示泛型型別定義,則為 true,否則為 false

範例

下列範例會顯示類型的相關信息,包括是否為泛型型別定義。 建構型別、泛型型別定義和一般型別的信息都會顯示。

C#
using System;
using System.Reflection;
using System.Collections.Generic;

public class Test
{
    private static void DisplayGenericTypeInfo(Type t)
    {
        Console.WriteLine("\r\n{0}", t);

        Console.WriteLine("\tIs this a generic type definition? {0}", 
            t.IsGenericTypeDefinition);

        Console.WriteLine("\tIs it a generic type? {0}", 
            t.IsGenericType);

        if (t.IsGenericType)
        {
            // If this is a generic type, display the type arguments.
            //
            Type[] typeArguments = t.GetGenericArguments();

            Console.WriteLine("\tList type arguments ({0}):", 
                typeArguments.Length);

            foreach (Type tParam in typeArguments)
            {
                // If this is a type parameter, display its
                // position.
                //
                if (tParam.IsGenericParameter)
                {
                    Console.WriteLine("\t\t{0}\t(unassigned - parameter position {1})",
                        tParam,
                        tParam.GenericParameterPosition);
                }
                else
                {
                    Console.WriteLine("\t\t{0}", tParam);
                }
            }
        }
    }

    public static void Main()
    {
        Console.WriteLine("\r\n--- Display information about a constructed type, its");
        Console.WriteLine("    generic type definition, and an ordinary type.");

        // Create a Dictionary of Test objects, using strings for the
        // keys.       
        Dictionary<string, Test> d = new Dictionary<string, Test>();

        // Display information for the constructed type and its generic
        // type definition.
        DisplayGenericTypeInfo(d.GetType());
        DisplayGenericTypeInfo(d.GetType().GetGenericTypeDefinition());

        // Display information for an ordinary type.
        DisplayGenericTypeInfo(typeof(string));
    }
}

/* This example produces the following output:

--- Display information about a constructed type, its
    generic type definition, and an ordinary type.

System.Collections.Generic.Dictionary[System.String,Test]
        Is this a generic type definition? False
        Is it a generic type? True
        List type arguments (2):
                System.String
                Test

System.Collections.Generic.Dictionary[TKey,TValue]
        Is this a generic type definition? True
        Is it a generic type? True
        List type arguments (2):
                TKey    (unassigned - parameter position 0)
                TValue  (unassigned - parameter position 1)

System.String
        Is this a generic type definition? False
        Is it a generic type? False
 */

備註

泛型型別定義是可從中建構其他型別的範本。 例如,從以 C# 語法表示的泛型型別定義 G<T> (;G(Of T)在 Visual Basic 或 generic <typename T> ref class G C++) 中,您可以使用包含Int32型別的泛型自變數清單呼叫 MakeGenericType 方法來建構和具現化類型 G<int>) (G(Of Integer)。 假設物件 Type 代表這個建構型別, GetGenericTypeDefinition 方法會再次取得泛型型別定義。

IsGenericTypeDefinition使用屬性來判斷您是否可以從目前的型別建立新的類型。 如果屬性傳IsGenericTypeDefinitiontrue回 ,您可以呼叫 MakeGenericType 方法來建立新的泛型型別。

如需泛型反映中所使用之規範的恆成立條件清單,請參閱 IsGenericType 屬性備註。

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱