英語で読む

次の方法で共有


MethodBuilder.MakeGenericMethod(Type[]) メソッド

定義

指定したジェネリック型引数を使用して、現在のジェネリック メソッド定義から構築されたジェネリック メソッドを返します。

C#
public override System.Reflection.MethodInfo MakeGenericMethod(params Type[] typeArguments);

パラメーター

typeArguments
Type[]

ジェネリック メソッドの型引数を表す Type オブジェクトの配列。

戻り値

指定したジェネリック型引数を使用して、現在のジェネリック メソッド定義から構築されたジェネリック メソッドを表す MethodInfo

次のコード例では、不完全な型の不完全なジェネリック メソッド定義から構築されたメソッドを作成します。

この例では、1 つの型を持つ一時的なアセンブリとモジュールを作成し、 メソッドを追加し、 メソッド Mを使用して型パラメーター T を追加してメソッドを DefineGenericParameters ジェネリックにします。 型パラメーターは、メソッドのパラメーターの型として使用され、戻り値の型としても使用されます。 ジェネリック メソッド定義には本文が指定されておらず、外側の型は完了していません。 MakeGenericMethod次に、 メソッドを使用して、構築されたメソッド M<String> (M(Of String) Visual Basic では ) を作成します。 メソッドによって返される の MethodInfo サブクラスでは、そのパラメーターに対するリフレクションが MakeGenericMethod 許可されないため、サンプル コードには出力がありません。

注意

を使用 MakeGenericMethodする別のコード例については、「」を参照してください DefineGenericParametersMakeGenericMethod は、ジェネリック型を使用するコードを出力するときにも広く使用されます。 「 方法: リフレクション出力を使用してジェネリック メソッドを定義する」を参照してください。

C#
using System;
using System.Reflection;
using System.Reflection.Emit;

class Example
{
    public static void Main()
    {
        // Define a transient dynamic assembly (only to run, not
        // to save) with one module and a type "Test".
        //
        AssemblyName aName = new AssemblyName("MyDynamic");
        AssemblyBuilder ab =
            AppDomain.CurrentDomain.DefineDynamicAssembly(
                aName,
                AssemblyBuilderAccess.Run);
        ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
        TypeBuilder tb = mb.DefineType("Test");

        // Add a public static method "M" to Test, and make it a
        // generic method with one type parameter named "T").
        //
        MethodBuilder meb = tb.DefineMethod("M",
            MethodAttributes.Public | MethodAttributes.Static);
        GenericTypeParameterBuilder[] typeParams =
            meb.DefineGenericParameters(new string[] { "T" });

        // Give the method one parameter, of type T, and a
        // return type of T.
        meb.SetParameters(typeParams);
        meb.SetReturnType(typeParams[0]);

        // Create a MethodInfo for M<string>, which can be used in
        // emitted code. This is possible even though the method
        // does not yet have a body, and the enclosing type is not
        // created.
        MethodInfo mi = meb.MakeGenericMethod(typeof(string));
        // Note that this is actually a subclass of MethodInfo,
        // which has rather limited capabilities -- for
        // example, you cannot reflect on its parameters.
    }
}

注釈

動的コードを出力する場合は、外側の型が完了する前に、 で MethodBuilder表されるジェネリック メソッド定義から構築されたメソッドの呼び出しを出力する必要がある場合があります。 メソッドを MakeGenericMethod 使用して、このような構築されたメソッドの を MethodInfo 作成し、出力された呼び出しで を MethodInfo 使用できます。

適用対象

製品 バージョン
.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 2.0 (package-provided), 2.1

こちらもご覧ください