英語で読む

次の方法で共有


Type.GetGenericTypeDefinition メソッド

定義

現在のジェネリック型を構築する元になるジェネリック型定義を表す Type オブジェクトを返します。

C#
public abstract Type GetGenericTypeDefinition();
C#
public virtual Type GetGenericTypeDefinition();

戻り値

現在の型を構築する元になるジェネリック型を表す Type オブジェクト。

例外

現在の型はジェネリック型ではありません。 つまり、IsGenericTypefalse を返します。

呼び出されたメソッドは基底クラスでサポートされていません。 派生クラスには実装を指定しなければなりません。

次のコード例では、通常のインスタンス作成を使用して構築された型のインスタンスを作成し、 メソッドと GetGenericTypeDefinition メソッドをGetType使用して、構築された型とジェネリック型の定義を取得します。 この例では、ジェネリックDictionary<TKey,TValue>型を使用します。構築された型は、文字列キーを持つ オブジェクトの TestDictionary<TKey,TValue>表します。

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

public class Test
{
    public static void Main()
    {
        Console.WriteLine("\r\n--- Get the generic type that defines a constructed type.");

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

        // Get a Type object representing the constructed type.
        //
        Type constructed = d.GetType();
        DisplayTypeInfo(constructed);

        Type generic = constructed.GetGenericTypeDefinition();
        DisplayTypeInfo(generic);
    }

    private static void DisplayTypeInfo(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);
        Type[] typeArguments = t.GetGenericArguments();
        Console.WriteLine("\tList type arguments ({0}):", typeArguments.Length);
        foreach (Type tParam in typeArguments)
        {
            Console.WriteLine("\t\t{0}", tParam);
        }
    }
}

/* This example produces the following output:

--- Get the generic type that defines a constructed type.

System.Collections.Generic.Dictionary`2[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`2[TKey,TValue]
        Is this a generic type definition? True
        Is it a generic type? True
        List type arguments (2):
                TKey
                TValue
 */

注釈

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

同じジェネリック型定義から同じ型引数を使用して 2 つの構築された型が作成された場合、 GetGenericTypeDefinition メソッドは両方の型に対して同じ Type オブジェクトを返します。

ジェネリック型定義を GetGenericTypeDefinition 既に表す オブジェクトで Type メソッドを呼び出すと、現在 Typeの が返されます。

重要

ジェネリック型の配列自体はジェネリックではありません。 C# コードまたは Visual Basic コードA<int>[] v;Dim v() As A(Of Integer)では、変数vの型はジェネリックではありません。 を呼び出すGetGenericTypeDefinition前に型がジェネリックかどうかを判断するには、 を使用IsGenericTypeします。

ジェネリック リフレクションで使用する用語に関する一定の条件の一覧については、IsGenericType プロパティの解説を参照してください。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください