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

注意

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

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

void main()
{
    // Define a transient dynamic assembly (only to run, not
    // to save) with one module and a type "Test".
    // 
    AssemblyName^ aName = gcnew 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);
    array<GenericTypeParameterBuilder^>^ typeParams = 
        meb->DefineGenericParameters(gcnew array<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(String::typeid);
    // Note that this is actually a subclass of MethodInfo, 
    // which has rather limited capabilities -- for
    // example, you cannot reflect on its parameters.
}
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 使用できます。

適用対象

こちらもご覧ください