英語で読む

次の方法で共有


Type.MakeGenericType(Type[]) メソッド

定義

型の配列の要素を現在のジェネリック型定義の型パラメーターで置き換え、結果の構築型を表す Type オブジェクトを返します。

C#
public abstract Type MakeGenericType(params Type[] typeArguments);
C#
public virtual Type MakeGenericType(params Type[] typeArguments);

パラメーター

typeArguments
Type[]

現在のジェネリック型の型パラメーターに置き換えられる型の配列。

戻り値

typeArguments の要素を現在のジェネリック型の型パラメーターで置き換えることによって作られる構築型を表す Type

例外

現在の型はジェネリック型の定義を表していません。 つまり、IsGenericTypeDefinitionfalse を返します。

typeArgumentsnullです。

または

typeArguments のどの要素も null です。

typeArguments 内の要素数は現在のジェネリック型定義の型パラメーター数と同じではありません。

または

typeArguments のいずれかの要素が、現在のジェネリック型の対応する型パラメーターに指定された制約を満たしていません。

または

typeArguments には、ポインター型 (IsPointertrue を返します)、参照渡し型 (IsByReftrue を返します)、または Void である要素が含まれています。

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

次の例では、 メソッドを MakeGenericType 使用して、 型のジェネリック型定義から構築された型を 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--- Create a constructed type from the generic Dictionary type.");

        // Create a type object representing the generic Dictionary 
        // type, by omitting the type arguments (but keeping the 
        // comma that separates them, so the compiler can infer the
        // number of type parameters).      
        Type generic = typeof(Dictionary<,>);
        DisplayTypeInfo(generic);

        // Create an array of types to substitute for the type
        // parameters of Dictionary. The key is of type string, and
        // the type to be contained in the Dictionary is Test.
        Type[] typeArgs = { typeof(string), typeof(Test) };

        // Create a Type object representing the constructed generic
        // type.
        Type constructed = generic.MakeGenericType(typeArgs);
        DisplayTypeInfo(constructed);

        // Compare the type objects obtained above to type objects
        // obtained using typeof() and GetGenericTypeDefinition().
        Console.WriteLine("\r\n--- Compare types obtained by different methods:");

        Type t = typeof(Dictionary<String, Test>);
        Console.WriteLine("\tAre the constructed types equal? {0}", t == constructed);
        Console.WriteLine("\tAre the generic types equal? {0}", 
            t.GetGenericTypeDefinition() == 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:

--- Create a constructed type from the generic Dictionary type.

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

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

--- Compare types obtained by different methods:
        Are the constructed types equal? True
        Are the generic types equal? True
 */

注釈

この API の詳細については、「 Type.MakeGenericType の補足 API 解説」を参照してください。

適用対象

製品 バージョン
.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, 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 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください