英語で読む

次の方法で共有


Type.IsGenericTypeDefinition プロパティ

定義

現在の Type が、他のジェネリック型を構築できるジェネリック型の定義を表しているかどうかを示す値を取得します。

C#
public virtual bool IsGenericTypeDefinition { get; }

プロパティ値

true オブジェクトがジェネリック型定義を表している場合は Type。それ以外の場合は 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
 */

注釈

ジェネリック型定義は、他の型を構築できるテンプレートです。 たとえば、ジェネリック型定義 G<T> (C# 構文で表されます。 G(Of T) Visual Basic または generic <typename T> ref class G C++ では) から、 型を含むInt32ジェネリック引数リストを使用して メソッドを呼び出MakeGenericTypeすことで、型 G<int> (G(Of Integer)Visual Basic では) を構築してインスタンス化できます。 Typeこの構築された型を表す オブジェクトを指定すると、 メソッドはGetGenericTypeDefinitionジェネリック型定義を再度取得します。

プロパティを IsGenericTypeDefinition 使用して、現在の型から新しい型を作成できるかどうかを判断します。 プロパティが を IsGenericTypeDefinition 返す true場合は、 メソッドを 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

こちらもご覧ください