ILGenerator.EmitCalli Method

Definition

Puts a Calli instruction onto the Microsoft intermediate language (MSIL) stream.

Overloads

EmitCalli(OpCode, CallingConvention, Type, Type[])

Puts a Calli instruction onto the Microsoft intermediate language (MSIL) stream, specifying an unmanaged calling convention for the indirect call.

EmitCalli(OpCode, CallingConventions, Type, Type[], Type[])

Puts a Calli instruction onto the Microsoft intermediate language (MSIL) stream, specifying a managed calling convention for the indirect call.

EmitCalli(OpCode, CallingConvention, Type, Type[])

Puts a Calli instruction onto the Microsoft intermediate language (MSIL) stream, specifying an unmanaged calling convention for the indirect call.

public:
 void EmitCalli(System::Reflection::Emit::OpCode opcode, System::Runtime::InteropServices::CallingConvention unmanagedCallConv, Type ^ returnType, cli::array <Type ^> ^ parameterTypes);
public void EmitCalli (System.Reflection.Emit.OpCode opcode, System.Runtime.InteropServices.CallingConvention unmanagedCallConv, Type returnType, Type[] parameterTypes);
member this.EmitCalli : System.Reflection.Emit.OpCode * System.Runtime.InteropServices.CallingConvention * Type * Type[] -> unit

Parameters

opcode
OpCode

The MSIL instruction to be emitted onto the stream. Must be Calli.

unmanagedCallConv
CallingConvention

The unmanaged calling convention to be used.

returnType
Type

The Type of the result.

parameterTypes
Type[]

The types of the required arguments to the instruction.

Examples

The following code sample demonstrates the contextual usage of the EmitCalli method to call an unmanaged type method external to the dynamic class.

MethodBuilder^ myMthdBuilder = myTypeBuilder->DefineMethod( "MyMethod",
                               MethodAttributes::Public,
                               returnType, mthdParamTypes );

// We will assume that an external unmanaged type "LegacyNumber" has been loaded, and
// that it has a method "ToString" which returns a String.

MethodInfo^ unmanagedMthdMI = Type::GetType( "LegacyNumber" )->GetMethod( "ToString" );
ILGenerator^ myMthdIL = myMthdBuilder->GetILGenerator();

// Code to emit various IL opcodes here ...

// Load a reference to the specific Object instance onto the stack.

myMthdIL->Emit( OpCodes::Ldc_I4, addrOfLegacyNumberObject );
myMthdIL->Emit( OpCodes::Ldobj, Type::GetType( "LegacyNumber" ) );

// Make the call to the unmanaged type method, telling it that the method is
// the member of a specific instance, to expect a String
// as a return value, and that there are no explicit parameters.
myMthdIL->EmitCalli( OpCodes::Calli,
                     System::Runtime::InteropServices::CallingConvention::ThisCall,
                     String::typeid,
                     gcnew array<Type^>( 0 ) );

// More IL code emission here ...
   MethodBuilder myMthdBuilder = myTypeBuilder.DefineMethod("MyMethod", 
                     MethodAttributes.Public,
                     returnType, mthdParamTypes);
                               
   // We will assume that an external unmanaged type "LegacyNumber" has been loaded, and
   // that it has a method "ToString" which returns a string.

   MethodInfo unmanagedMthdMI = Type.GetType("LegacyNumber").GetMethod("ToString");
   ILGenerator myMthdIL = myMthdBuilder.GetILGenerator();

   // Code to emit various IL opcodes here ...

   // Load a reference to the specific object instance onto the stack.

   myMthdIL.Emit(OpCodes.Ldc_I4, addrOfLegacyNumberObject);
   myMthdIL.Emit(OpCodes.Ldobj, Type.GetType("LegacyNumber"));

   // Make the call to the unmanaged type method, telling it that the method is
   // the member of a specific instance, to expect a string 
   // as a return value, and that there are no explicit parameters.
   myMthdIL.EmitCalli(OpCodes.Calli, 
              System.Runtime.InteropServices.CallingConvention.ThisCall,
                  typeof(string),
              new Type[] {});

   // More IL code emission here ...
   
    Dim myMthdBuilder As MethodBuilder = myTypeBuilder.DefineMethod("MyMethod", _
                    MethodAttributes.Public, _
                    returnType, mthdParamTypes)
  
    ' We will assume that an external unmanaged type "LegacyNumber" has been loaded, and
    ' that it has a method "ToString" which returns a string.

    Dim unmanagedMthdMI As MethodInfo = Type.GetType("LegacyNumber").GetMethod("ToString")
    Dim myMthdIL As ILGenerator = myMthdBuilder.GetILGenerator()
  
    ' Code to emit various IL opcodes here ...
    ' Load a reference to the specific object instance onto the stack.

    myMthdIL.Emit(OpCodes.Ldc_I4, addrOfLegacyNumberObject)
    myMthdIL.Emit(OpCodes.Ldobj, Type.GetType("LegacyNumber"))
  
    ' Make the call to the unmanaged type method, telling it that the method is
    ' the member of a specific instance, to expect a string 
    ' as a return value, and that there are no explicit parameters.

    myMthdIL.EmitCalli(OpCodes.Calli, System.Runtime.InteropServices.CallingConvention.ThisCall, _
                  GetType(String), New Type() {})

' More IL code emission here ...

Remarks

Use EmitCalli to put a Calli instruction onto the stream. Do not use Emit.

EmitCalli(OpCode, CallingConventions, Type, Type[], Type[])

Puts a Calli instruction onto the Microsoft intermediate language (MSIL) stream, specifying a managed calling convention for the indirect call.

public:
 void EmitCalli(System::Reflection::Emit::OpCode opcode, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, cli::array <Type ^> ^ optionalParameterTypes);
public void EmitCalli (System.Reflection.Emit.OpCode opcode, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type[] optionalParameterTypes);
member this.EmitCalli : System.Reflection.Emit.OpCode * System.Reflection.CallingConventions * Type * Type[] * Type[] -> unit

Parameters

opcode
OpCode

The MSIL instruction to be emitted onto the stream. Must be Calli.

callingConvention
CallingConventions

The managed calling convention to be used.

returnType
Type

The Type of the result.

parameterTypes
Type[]

The types of the required arguments to the instruction.

optionalParameterTypes
Type[]

The types of the optional arguments for varargs calls.

Exceptions

optionalParameterTypes is not null, but callingConvention does not include the VarArgs flag.

Remarks

Use EmitCalli to put a Calli instruction onto the stream. Do not use Emit.

If optionalParameterTypes specifies optional arguments, callingConvention must include the CallingConventions.VarArgs flag.

Applies to