ILGenerator.EmitCall(OpCode, MethodInfo, Type[]) 메서드

정의

call 또는 callvirt 명령을 MSIL(Microsoft intermediate language) 스트림에 배치하여 varargs 메서드를 호출합니다.

public:
 virtual void EmitCall(System::Reflection::Emit::OpCode opcode, System::Reflection::MethodInfo ^ methodInfo, cli::array <Type ^> ^ optionalParameterTypes);
public:
 abstract void EmitCall(System::Reflection::Emit::OpCode opcode, System::Reflection::MethodInfo ^ methodInfo, cli::array <Type ^> ^ optionalParameterTypes);
public:
 void EmitCall(System::Reflection::Emit::OpCode opcode, System::Reflection::MethodInfo ^ methodInfo, cli::array <Type ^> ^ optionalParameterTypes);
public virtual void EmitCall (System.Reflection.Emit.OpCode opcode, System.Reflection.MethodInfo methodInfo, Type[]? optionalParameterTypes);
public abstract void EmitCall (System.Reflection.Emit.OpCode opcode, System.Reflection.MethodInfo methodInfo, Type[]? optionalParameterTypes);
public virtual void EmitCall (System.Reflection.Emit.OpCode opcode, System.Reflection.MethodInfo methodInfo, Type[] optionalParameterTypes);
public void EmitCall (System.Reflection.Emit.OpCode opcode, System.Reflection.MethodInfo methodInfo, Type[] optionalParameterTypes);
abstract member EmitCall : System.Reflection.Emit.OpCode * System.Reflection.MethodInfo * Type[] -> unit
override this.EmitCall : System.Reflection.Emit.OpCode * System.Reflection.MethodInfo * Type[] -> unit
abstract member EmitCall : System.Reflection.Emit.OpCode * System.Reflection.MethodInfo * Type[] -> unit
member this.EmitCall : System.Reflection.Emit.OpCode * System.Reflection.MethodInfo * Type[] -> unit
Public Overridable Sub EmitCall (opcode As OpCode, methodInfo As MethodInfo, optionalParameterTypes As Type())
Public MustOverride Sub EmitCall (opcode As OpCode, methodInfo As MethodInfo, optionalParameterTypes As Type())
Public Sub EmitCall (opcode As OpCode, methodInfo As MethodInfo, optionalParameterTypes As Type())

매개 변수

opcode
OpCode

스트림에 내보낼 MSIL 명령입니다. Call, Callvirt 또는 Newobj여야 합니다.

methodInfo
MethodInfo

호출할 varargs 메서드입니다.

optionalParameterTypes
Type[]

메서드가 varargs 메서드이면 선택적 인수의 형식이고, 그렇지 않으면 null입니다.

예외

opcode에서 메서드 호출을 지정하지 않습니다.

methodInfo이(가) null인 경우

메서드에 대한 호출 규칙이 varargs가 아니고 선택적 매개 변수 형식이 제공됩니다. 이 예외는 .NET Framework 버전 1.0 및 1.1에서 throw됩니다. 후속 버전에서는 예외가 throw되지 않습니다.

예제

다음 코드 예제에서는 메서드와 메서드를 varargs 호출하는 메서드라는 두 가지 메서드를 내보낸다 varargs . 메서드는 EmitCall 메서드에 대한 호출을 내보내는 varargs 데 사용됩니다.

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

void main() 
{
    String^ name = "InMemory";

    AssemblyBuilder^ asmBldr = 
       AppDomain::CurrentDomain->DefineDynamicAssembly(gcnew AssemblyName(name), 
          AssemblyBuilderAccess::Run);
    ModuleBuilder^ modBldr = asmBldr->DefineDynamicModule(name); 

    TypeBuilder^ tb = modBldr->DefineType("DemoVararg");

    // Create a vararg method with no return value and one 
    // string argument. (The string argument type is the only
    // element of an array of Type objects.)
    //
    MethodBuilder^ mb1 = tb->DefineMethod("VarargMethod",
        MethodAttributes::Public | MethodAttributes::Static,
        CallingConventions::VarArgs,
        nullptr, 
        gcnew array<Type^> { String::typeid });

    ILGenerator^ il1 = mb1->GetILGenerator();

    LocalBuilder^ locAi = il1->DeclareLocal(ArgIterator::typeid);
    LocalBuilder^ locNext = il1->DeclareLocal(bool::typeid);

    Label labelCheckCondition = il1->DefineLabel();
    Label labelNext = il1->DefineLabel();

    // Load the fixed argument and print it.
    il1->Emit(OpCodes::Ldarg_0);
    il1->Emit(OpCodes::Call, Console::typeid->GetMethod("Write", 
         gcnew array<Type^> { String::typeid }));

    // Load the address of the local variable represented by
    // locAi, which will hold the ArgIterator.
    il1->Emit(OpCodes::Ldloca_S, locAi);

    // Load the address of the argument list, and call the 
    // ArgIterator constructor that takes an array of runtime
    // argument handles. 
    il1->Emit(OpCodes::Arglist);
    il1->Emit(OpCodes::Call, ArgIterator::typeid->GetConstructor(
         gcnew array<Type^> { RuntimeArgumentHandle::typeid }));

    // Enter the loop at the point where the remaining argument
    // count is tested.
    il1->Emit(OpCodes::Br_S, labelCheckCondition);

    // At the top of the loop, call GetNextArg to get the next 
    // argument from the ArgIterator. Convert the typed reference
    // to an object reference and write the object to the console.
    il1->MarkLabel(labelNext);
    il1->Emit(OpCodes::Ldloca_S, locAi);
    il1->Emit(OpCodes::Call, ArgIterator::typeid->GetMethod("GetNextArg", Type::EmptyTypes));
    il1->Emit(OpCodes::Call, TypedReference::typeid->GetMethod("ToObject"));
    il1->Emit(OpCodes::Call, Console::typeid->GetMethod("Write", 
         gcnew array<Type^> { Object::typeid }));

    il1->MarkLabel(labelCheckCondition);
    il1->Emit(OpCodes::Ldloca_S, locAi);
    il1->Emit(OpCodes::Call, ArgIterator::typeid->GetMethod("GetRemainingCount"));

    // If the remaining count is greater than zero, go to
    // the top of the loop.
    il1->Emit(OpCodes::Ldc_I4_0);
    il1->Emit(OpCodes::Cgt);
    il1->Emit(OpCodes::Stloc_1);
    il1->Emit(OpCodes::Ldloc_1);
    il1->Emit(OpCodes::Brtrue_S, labelNext);

    il1->Emit(OpCodes::Ret);

    // Create a method that contains a call to the vararg 
    // method.
    MethodBuilder^ mb2 = tb->DefineMethod("CallVarargMethod",
        MethodAttributes::Public | MethodAttributes::Static,
        CallingConventions::Standard,
        nullptr, Type::EmptyTypes);

    ILGenerator^ il2 = mb2->GetILGenerator();

    // Push arguments on the stack: one for the fixed string
    // parameter, and two for the list.
    il2->Emit(OpCodes::Ldstr, "Hello ");
    il2->Emit(OpCodes::Ldstr, "world ");
    il2->Emit(OpCodes::Ldc_I4, 2006);

    // Call the vararg method, specifying the types of the 
    // arguments in the list.
    il2->EmitCall(OpCodes::Call, mb1, 
        gcnew array<Type^> { String::typeid, int::typeid });

    il2->Emit(OpCodes::Ret);

    Type^ type = tb->CreateType();
    type->GetMethod("CallVarargMethod")->Invoke(nullptr, nullptr);
}

/* This code example produces the following output:

Hello world 2006
 */
using System;
using System.Reflection;
using System.Reflection.Emit;

class Example
{
    static void Main()
    {
        string name = "InMemory";

        AssemblyBuilder asmBldr =
           AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(name),
              AssemblyBuilderAccess.Run);
        ModuleBuilder modBldr = asmBldr.DefineDynamicModule(name);

        TypeBuilder tb = modBldr.DefineType("DemoVararg");

        // Create a vararg method with no return value and one
        // string argument. (The string argument type is the only
        // element of an array of Type objects.)
        //
        MethodBuilder mb1 = tb.DefineMethod("VarargMethod",
            MethodAttributes.Public | MethodAttributes.Static,
            CallingConventions.VarArgs,
            null,
            new Type[] { typeof(string) });

        ILGenerator il1 = mb1.GetILGenerator();

        LocalBuilder locAi = il1.DeclareLocal(typeof(ArgIterator));
        LocalBuilder locNext = il1.DeclareLocal(typeof(bool));

        Label labelCheckCondition = il1.DefineLabel();
        Label labelNext = il1.DefineLabel();

        // Load the fixed argument and print it.
        il1.Emit(OpCodes.Ldarg_0);
        il1.Emit(OpCodes.Call, typeof(Console).GetMethod("Write", new Type[] { typeof(string) }));

        // Load the address of the local variable represented by
        // locAi, which will hold the ArgIterator.
        il1.Emit(OpCodes.Ldloca_S, locAi);

        // Load the address of the argument list, and call the
        // ArgIterator constructor that takes an array of runtime
        // argument handles.
        il1.Emit(OpCodes.Arglist);
        il1.Emit(OpCodes.Call, typeof(ArgIterator).GetConstructor(new Type[] { typeof(RuntimeArgumentHandle) }));

        // Enter the loop at the point where the remaining argument
        // count is tested.
        il1.Emit(OpCodes.Br_S, labelCheckCondition);

        // At the top of the loop, call GetNextArg to get the next
        // argument from the ArgIterator. Convert the typed reference
        // to an object reference and write the object to the console.
        il1.MarkLabel(labelNext);
        il1.Emit(OpCodes.Ldloca_S, locAi);
        il1.Emit(OpCodes.Call, typeof(ArgIterator).GetMethod("GetNextArg", Type.EmptyTypes));
        il1.Emit(OpCodes.Call, typeof(TypedReference).GetMethod("ToObject"));
        il1.Emit(OpCodes.Call, typeof(Console).GetMethod("Write", new Type[] { typeof(object) }));

        il1.MarkLabel(labelCheckCondition);
        il1.Emit(OpCodes.Ldloca_S, locAi);
        il1.Emit(OpCodes.Call, typeof(ArgIterator).GetMethod("GetRemainingCount"));

        // If the remaining count is greater than zero, go to
        // the top of the loop.
        il1.Emit(OpCodes.Ldc_I4_0);
        il1.Emit(OpCodes.Cgt);
        il1.Emit(OpCodes.Stloc_1);
        il1.Emit(OpCodes.Ldloc_1);
        il1.Emit(OpCodes.Brtrue_S, labelNext);

        il1.Emit(OpCodes.Ret);

        // Create a method that contains a call to the vararg
        // method.
        MethodBuilder mb2 = tb.DefineMethod("CallVarargMethod",
            MethodAttributes.Public | MethodAttributes.Static,
            CallingConventions.Standard,
            typeof(void), Type.EmptyTypes);
        ILGenerator il2 = mb2.GetILGenerator();

        // Push arguments on the stack: one for the fixed string
        // parameter, and two for the list.
        il2.Emit(OpCodes.Ldstr, "Hello ");
        il2.Emit(OpCodes.Ldstr, "world ");
        il2.Emit(OpCodes.Ldc_I4, 2006);

        // Call the vararg method, specifying the types of the
        // arguments in the list.
        il2.EmitCall(OpCodes.Call, mb1, new Type[] { typeof(string), typeof(int) });

        il2.Emit(OpCodes.Ret);

        Type type = tb.CreateType();
        type.GetMethod("CallVarargMethod").Invoke(null, null);
    }
}

/* This code example produces the following output:

Hello world 2006
 */
Imports System.Reflection
Imports System.Reflection.Emit

Class Example
    
    Shared Sub Main() 

        Dim name As String = "InMemory"
        
        Dim asmBldr As AssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly( _
            New AssemblyName(name), AssemblyBuilderAccess.Run)
        Dim modBldr As ModuleBuilder = asmBldr.DefineDynamicModule(name)
        
        Dim tb As TypeBuilder = modBldr.DefineType("DemoVararg")
        
        ' Create a vararg method with no return value and one 
        ' string argument. (The string argument type is the only
        ' element of an array of Type objects.)
        '
        Dim mb1 As MethodBuilder = tb.DefineMethod("VarargMethod", _
            MethodAttributes.Public Or MethodAttributes.Static, _
            CallingConventions.VarArgs, _
            Nothing, _
            New Type() {GetType(String)})
        
        Dim il1 As ILGenerator = mb1.GetILGenerator()
        
        Dim locAi As LocalBuilder = il1.DeclareLocal(GetType(ArgIterator))
        Dim locNext As LocalBuilder = il1.DeclareLocal(GetType(Boolean))
        
        Dim labelCheckCondition As Label = il1.DefineLabel()
        Dim labelNext As Label = il1.DefineLabel()
        
        ' Load the fixed argument and print it.
        il1.Emit(OpCodes.Ldarg_0)
        il1.Emit(OpCodes.Call, GetType(Console).GetMethod("Write", _
            New Type() {GetType(String)}))
        
        ' Load the address of the local variable represented by
        ' locAi, which will hold the ArgIterator.
        il1.Emit(OpCodes.Ldloca_S, locAi)
        
        ' Load the address of the argument list, and call the 
        ' ArgIterator constructor that takes an array of runtime
        ' argument handles. 
        il1.Emit(OpCodes.Arglist)
        il1.Emit(OpCodes.Call, GetType(ArgIterator).GetConstructor( _
            New Type() {GetType(RuntimeArgumentHandle)}))
        
        ' Enter the loop at the point where the remaining argument
        ' count is tested.
        il1.Emit(OpCodes.Br_S, labelCheckCondition)
        
        ' At the top of the loop, call GetNextArg to get the next 
        ' argument from the ArgIterator. Convert the typed reference
        ' to an object reference and write the object to the console.
        il1.MarkLabel(labelNext)
        il1.Emit(OpCodes.Ldloca_S, locAi)
        il1.Emit(OpCodes.Call, _
            GetType(ArgIterator).GetMethod("GetNextArg", Type.EmptyTypes))
        il1.Emit(OpCodes.Call, GetType(TypedReference).GetMethod("ToObject"))
        il1.Emit(OpCodes.Call, _
            GetType(Console).GetMethod("Write", New Type() {GetType(Object)}))
        
        il1.MarkLabel(labelCheckCondition)
        il1.Emit(OpCodes.Ldloca_S, locAi)
        il1.Emit(OpCodes.Call, _
            GetType(ArgIterator).GetMethod("GetRemainingCount"))
        
        ' If the remaining count is greater than zero, go to
        ' the top of the loop.
        il1.Emit(OpCodes.Ldc_I4_0)
        il1.Emit(OpCodes.Cgt)
        il1.Emit(OpCodes.Stloc_1)
        il1.Emit(OpCodes.Ldloc_1)
        il1.Emit(OpCodes.Brtrue_S, labelNext)
        
        il1.Emit(OpCodes.Ret)
        
        ' Create a method that contains a call to the vararg 
        ' method.
        Dim mb2 As MethodBuilder = tb.DefineMethod("CallVarargMethod", _
            MethodAttributes.Public Or MethodAttributes.Static, _
            CallingConventions.Standard, _
            Nothing, _
            Type.EmptyTypes)

        Dim il2 As ILGenerator = mb2.GetILGenerator()
        
        ' Push arguments on the stack: one for the fixed string
        ' parameter, and two for the list.
        il2.Emit(OpCodes.Ldstr, "Hello ")
        il2.Emit(OpCodes.Ldstr, "world ")
        il2.Emit(OpCodes.Ldc_I4, 2006)
        
        ' Call the vararg method, specifying the types of the 
        ' arguments in the list.
        il2.EmitCall(OpCodes.Call, mb1, _
            New Type() {GetType(String), GetType(Integer)})
        
        il2.Emit(OpCodes.Ret)
        
        Dim myType As Type = tb.CreateType()
        myType.GetMethod("CallVarargMethod").Invoke(Nothing, Nothing)
    
    End Sub 
End Class 

' This code example produces the following output:
'
'Hello world 2006
'

설명

메서드는 EmitCall 변수 인수의 매개 변수 형식을 지정하는 varargs 메서드의 Emit 오버로드가 없으므로 메서드 호출을 내보내는 데 사용됩니다.

호출 규칙을 사용하지 않는 메서드에 대한 호출을 VarArgs 내보내려면 메서드 오버로드를 Emit(OpCode, MethodInfo) 사용합니다.

EmitCall 가 아닌 메서드에 대해 선택적 매개 변수 형식을 지정하면 메서드가 예외를 throw하지 않습니다varargs. InvalidProgramException 는 호출이 실행될 때 throw됩니다.

적용 대상