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 입니다.

예제

다음 코드 예제에서는 불완전한 형식의 불완전한 제네릭 메서드 정의에서 생성된 메서드를 만듭니다.

이 예제에서는 단일 형식으로 임시 어셈블리 및 모듈을 만들고, 메서드를 추가하고, 메서드 M를 사용하여 DefineGenericParameters 형식 매개 변수 T를 추가하여 메서드를 제네릭으로 만듭니다. 형식 매개 변수는 메서드 매개 변수의 형식으로 사용되며 반환 형식으로도 사용됩니다. 제네릭 메서드 정의에는 본문이 제공되지 않으며 바깥쪽 형식이 완료되지 않습니다. 그런 다음 메서드를 MakeGenericMethod 사용하여 생성된 메서드 M<String> (M(Of String) Visual Basic의 경우)를 만듭니다. 메서드에서 반환된 의 MethodInfo 서브클래스는 해당 매개 변수에 대한 리플렉션을 MakeGenericMethod 허용하지 않으므로 예제 코드에는 출력이 없습니다.

참고

를 사용하는 MakeGenericMethod다른 코드 예제는 를 참조하세요 DefineGenericParameters. MakeGenericMethod 는 제네릭 형식을 사용하는 코드를 내보낸 경우에도 광범위하게 사용됩니다. 방법: 리플렉션 내보내기를 사용하여 제네릭 메서드 정의를 참조하세요.

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 수 있습니다.

적용 대상

추가 정보