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[])

Source:
ILGenerator.cs
Source:
ILGenerator.cs
Source:
ILGenerator.cs

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

public virtual void EmitCalli (System.Reflection.Emit.OpCode opcode, System.Runtime.InteropServices.CallingConvention unmanagedCallConv, Type? returnType, Type[]? parameterTypes);
public abstract void EmitCalli (System.Reflection.Emit.OpCode opcode, System.Runtime.InteropServices.CallingConvention unmanagedCallConv, Type? returnType, Type[]? parameterTypes);
public virtual void EmitCalli (System.Reflection.Emit.OpCode opcode, System.Runtime.InteropServices.CallingConvention unmanagedCallConv, Type returnType, Type[] parameterTypes);
public void EmitCalli (System.Reflection.Emit.OpCode opcode, System.Runtime.InteropServices.CallingConvention unmanagedCallConv, Type returnType, Type[] parameterTypes);

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,
               typeof(string),
           new Type[] {});

// More IL code emission here ...

Remarks

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

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.1

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

Source:
ILGenerator.cs
Source:
ILGenerator.cs
Source:
ILGenerator.cs

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

public virtual void EmitCalli (System.Reflection.Emit.OpCode opcode, System.Reflection.CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes, Type[]? optionalParameterTypes);
public abstract void EmitCalli (System.Reflection.Emit.OpCode opcode, System.Reflection.CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes, Type[]? optionalParameterTypes);
public virtual void EmitCalli (System.Reflection.Emit.OpCode opcode, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type[] optionalParameterTypes);
public void EmitCalli (System.Reflection.Emit.OpCode opcode, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type[] optionalParameterTypes);

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

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1