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

定義

指定したジェネリック型引数を使用して、現在のジェネリック メソッドの定義から構築されたジェネリック メソッドを返します。Returns a generic method constructed from the current generic method definition using the specified generic type arguments.

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 オブジェクトの配列。An array of Type objects that represent the type arguments for the generic method.

戻り値

指定したジェネリック型引数を使用して、現在のジェネリック メソッド定義から構築されたジェネリック メソッドを表す MethodInfoA MethodInfo representing the generic method constructed from the current generic method definition using the specified generic type arguments.

不完全な型のジェネリックメソッドの定義から構築されたメソッドを作成するコード例を次に示します。The following code example creates a constructed method from an incomplete generic method definition in an incomplete type.

この例では、1つの型を持つ一時的なアセンブリとモジュールを作成し、メソッド Mを追加し、DefineGenericParameters メソッドを使用して型パラメーター T を追加することによって、メソッドをジェネリックにします。The example creates a transient assembly and module with a single type, adds a method M, and makes the method generic by adding a type parameter T using the DefineGenericParameters method. 型パラメーターは、メソッドのパラメーターの型として使用されるほか、戻り値の型としても使用されます。The type parameter is used as the type of the method's parameter, and also as its return type. ジェネリックメソッド定義に本文が指定されておらず、外側の型が完了していません。The generic method definition is not given a body, and the enclosing type is not completed. 次に、MakeGenericMethod メソッドを使用して、構築されたメソッドを M<String> (M(Of String) Visual Basic) します。The MakeGenericMethod method is then used to make the constructed method M<String> (M(Of String) in Visual Basic). この例のコードには出力がありません。 MakeGenericMethod メソッドによって返される MethodInfo のサブクラスでは、パラメーターに対するリフレクションが許可されていないためです。The example code has no output, because the subclass of MethodInfo returned by the MakeGenericMethod method does not allow reflection over its parameters.

注意

MakeGenericMethodを使用する別のコード例については、「DefineGenericParameters」を参照してください。For another code example that uses MakeGenericMethod, see DefineGenericParameters. MakeGenericMethod は、ジェネリック型を使用するコードを出力するときにも広く使用されます。MakeGenericMethod is also used extensively when emitting code that uses generic types. 方法:リフレクション出力を使用してジェネリック メソッドを定義する」を参照してください。See How to: Define a Generic Method with Reflection Emit.

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によって表されるジェネリックメソッド定義から構築されたメソッドの呼び出しを出力する必要がある場合があります。When you are emitting dynamic code, you might need to emit a call to a method constructed from the generic method definition represented by a MethodBuilder, before the enclosing type has been completed. MakeGenericMethod メソッドを使用して、このように構築されたメソッドの MethodInfo を作成し、出力される呼び出しの MethodInfo を使用できます。You can use the MakeGenericMethod method to create a MethodInfo for such a constructed method, and use the MethodInfo in the emitted call.

適用対象

こちらもご覧ください