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

定義

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

public:
 override System::Reflection::MethodInfo ^ MakeGenericMethod(... cli::array <Type ^> ^ typeArguments);
public override System.Reflection.MethodInfo MakeGenericMethod(params Type[] typeArguments);
override this.MakeGenericMethod : Type[] -> System.Reflection.MethodInfo
Public Overrides Function MakeGenericMethod (ParamArray typeArguments As Type()) As MethodInfo

パラメーター

typeArguments
Type[]

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

返品

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

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

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

Note

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

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.
    }
}
Imports System.Reflection
Imports System.Reflection.Emit

Class Example

    Public Shared Sub Main()
    
        ' Define a transient dynamic assembly (only to run, not
        ' to save) with one module and a type "Test".
        ' 
        Dim aName As AssemblyName = New AssemblyName("MyDynamic")
        Dim ab As AssemblyBuilder = _
            AppDomain.CurrentDomain.DefineDynamicAssembly( _
                aName, _
                AssemblyBuilderAccess.Run)
        Dim mb As ModuleBuilder = ab.DefineDynamicModule(aName.Name)
        Dim tb As TypeBuilder = mb.DefineType("Test")

        ' Add a Public Shared method "M" to Test, and make it a
        ' generic method with one type parameter named "T").
        '
        Dim meb As MethodBuilder = tb.DefineMethod("M", _
            MethodAttributes.Public Or MethodAttributes.Static)
        Dim typeParams() As GenericTypeParameterBuilder = _
            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(Of 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.
        Dim mi As MethodInfo = _
            meb.MakeGenericMethod(GetType(String))
        ' Note that this is actually a subclass of MethodInfo, 
        ' which has rather limited capabilities -- for
        ' example, you cannot reflect on its parameters.
    End Sub
End Class

注釈

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

適用対象

こちらもご覧ください