Language

Type.GetGenericTypeDefinition メソッド

定義

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

public:
 abstract Type ^ GetGenericTypeDefinition();
public:
 virtual Type ^ GetGenericTypeDefinition();
public abstract Type GetGenericTypeDefinition();
public virtual Type GetGenericTypeDefinition();
abstract member GetGenericTypeDefinition : unit -> Type
abstract member GetGenericTypeDefinition : unit -> Type
override this.GetGenericTypeDefinition : unit -> Type
Public MustOverride Function GetGenericTypeDefinition () As Type
Public Overridable Function GetGenericTypeDefinition () As Type

返品

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

例外

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

呼び出されたメソッドは、基底クラスではサポートされていません。 派生クラスは実装を提供する必要があります。

例

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

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
 */
open System
open System.Collections.Generic

type Test() = class end

let displayTypeInfo (t: Type) =
    printfn $"\n{t}"
    printfn $"\tIs this a generic type definition? {t.IsGenericTypeDefinition}"
    printfn $"\tIs it a generic type? {t.IsGenericType}"
    let typeArguments = t.GetGenericArguments()
    printfn $"\tList type arguments ({typeArguments.Length}):"
    for tParam in typeArguments do
        printfn $"\t\t{tParam}"

printfn "\n--- Get the generic type that defines a constructed type."

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

// Get a Type object representing the constructed type.
let constructed = d.GetType()
displayTypeInfo constructed

let generic = constructed.GetGenericTypeDefinition()
displayTypeInfo generic


(* 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
 *)
Imports System.Reflection
Imports System.Collections.Generic

Public Class Test
    Public Shared Sub Main() 
        Console.WriteLine(vbCrLf & "--- Get the generic type that defines a constructed type.")
        
        ' Create a Dictionary of Test objects, using strings for the
        ' keys.
        Dim d As New Dictionary(Of String, Test)
        
        ' Get a Type object representing the constructed type.
        '
        Dim constructed As Type = d.GetType()
        DisplayTypeInfo(constructed)
        
        Dim generic As Type = constructed.GetGenericTypeDefinition()
        DisplayTypeInfo(generic)
    End Sub
    
    Private Shared Sub DisplayTypeInfo(ByVal t As Type) 
        Console.WriteLine(vbCrLf & t.ToString())
        Console.WriteLine(vbTab & "Is this a generic type definition? " _
            & t.IsGenericTypeDefinition)
        Console.WriteLine(vbTab & "Is it a generic type? " _
            & t.IsGenericType)
        Dim typeArguments As Type() = t.GetGenericArguments()
        Console.WriteLine(vbTab & "List type arguments (" _
            & typeArguments.Length & "):")
        For Each tParam As Type In typeArguments
            Console.WriteLine(vbTab & vbTab & tParam.ToString())
        Next tParam
    End Sub
End Class

' 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
'

注釈

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

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

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

Important

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

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

適用対象

こちらもご覧ください