Skip to content

ILWeaver Class

An API for manipulating CIL method bodies. See https://monodetour.github.io/ilhooking/introduction/

C#
public class ILWeaver

Inheritance object

Constructs a new ILWeaver instance for manipulating the target CIL method body. See https://monodetour.github.io/ilhooking/introduction/

C#
public ILWeaver(ILManipulationInfo il)

il ILManipulationInfo

Create a new ILWeaver for the current ILManipulationInfo with state copied optionally.

C#
public ILWeaver(ILWeaver weaver, bool copyState = true)

weaver ILWeaver

copyState bool

A manipulation info containing the MonoMod.Cil.ILContext intended for manipulation and an untouched MonoMod.Cil.ILContext useful for observing the untouched state, including the original method as a MethodBase if it exists.

C#
public ILManipulationInfo ManipulationInfo { get; }

An IL manipulation “context” with various helpers and direct access to the MethodBody.

C#
public ILContext Context { get; }

The manipulated method’s IL processor.

C#
public ILProcessor IL { get; }

The manipulated method body.

C#
public MethodBody Body { get; }

The manipulated method.

C#
public MethodDefinition Method { get; }

The manipulated method instructions.

C#
public Collection<Instruction> Instructions { get; }

The instruction this weaver currently points to.

C#
public Instruction Current { get; set; }

Even if the method body would have no instructions, ILWeaver creates a temporary instruction on the method body to ensure this can always be used as a reference for inserting new instructions.

Setter is CurrentTo(Instruction).
For replacing the current instruction, see ReplaceCurrent(Instruction)

The instruction before what this weaver currently points to.

C#
public Instruction Previous { get; }

The instruction after what this weaver currently points to.

C#
public Instruction Next { get; }

This is not equivalent to MonoMod.Cil.ILCursor.Next. The equivalent would be Current.

Gets the first instruction in the method body.

C#
public Instruction First { get; }

Gets the last instruction in the method body.

C#
public Instruction Last { get; }

The index of the instruction on Current

C#
public int Index { get; set; }

A negative index will loop back. Setter uses CurrentTo(int) which can throw.

Filter for which channels this log source logs to.

C#
public MonoDetourLogger.LogChannel LogFilter { get; set; }

Create a new ILWeaver for the current ILManipulationInfo using the ILWeaver(ILWeaver, bool) constructor.
Does not copy state.

C#
public ILWeaver New()

ILWeaver
A new ILWeaver for the current ILManipulationInfo.

Create a clone of the ILWeaver using the ILWeaver(ILWeaver, bool) constructor.
State is copied.

C#
public ILWeaver Clone()

ILWeaver
A clone of the ILWeaver.

Declare a new local variable on the target method.

A local variable can be assigned with a stloc instruction and loaded using an ldloc instruction. To assign or load this local variable, the returned VariableDefinition or its index should be used as the Operand.

C#
public VariableDefinition DeclareVariable(Type type)

type Type
The type of the local variable.

VariableDefinition
A new local variable.

DeclareVariable(Type, out VariableDefinition)

Section titled “DeclareVariable(Type, out VariableDefinition)”

Declare a new local variable on the target method.

A local variable can be assigned with a stloc instruction and loaded using an ldloc instruction. To assign or load this local variable, the returned VariableDefinition or its index should be used as the Operand.

C#
public ILWeaver DeclareVariable(Type type, out VariableDefinition variableDefinition)

type Type
The type of the local variable.

variableDefinition VariableDefinition
A new local variable.

ILWeaver
This ILWeaver.

HandlerCreateCatch(Type?, out WeaverExceptionCatchHandler)

Section titled “HandlerCreateCatch(Type?, out WeaverExceptionCatchHandler)”

Create a new IWeaverExceptionHandler to be assigned its ranges. Note that certain values can be figured out implicitly such as HandlerStart when TryEnd is defined.

The IWeaverExceptionHandler will automatically be applied to the method body after your manipulator method is finished.

C#
public ILWeaver HandlerCreateCatch(Type? catchType, out WeaverExceptionCatchHandler handler)

catchType Type
The types of Exceptions that should be catched. If left null, Exception is used.

handler WeaverExceptionCatchHandler
The created IWeaverExceptionHandler to be configured.

ILWeaver
The new exception handler instance.

See WeaverExceptionCatchHandler for more information about this handler type.

HandlerCreateFilter(Type?, out WeaverExceptionFilterHandler)

Section titled “HandlerCreateFilter(Type?, out WeaverExceptionFilterHandler)”

Create a new IWeaverExceptionHandler to be assigned its ranges. Note that certain values can be figured out implicitly such as HandlerStart when TryEnd is defined.

The IWeaverExceptionHandler will automatically be applied to the method body after your manipulator method is finished.

C#
public ILWeaver HandlerCreateFilter(Type? catchType, out WeaverExceptionFilterHandler handler)

catchType Type
The types of Exceptions that should be catched. If left null, Exception is used.

handler WeaverExceptionFilterHandler
The created IWeaverExceptionHandler to be configured.

ILWeaver
The new exception handler instance.

See WeaverExceptionFilterHandler for more information about this handler type.

HandlerCreateFinally(out WeaverExceptionFinallyHandler)

Section titled “HandlerCreateFinally(out WeaverExceptionFinallyHandler)”

Create a new IWeaverExceptionHandler to be assigned its ranges. Note that certain values can be figured out implicitly such as HandlerStart when TryEnd is defined.

The IWeaverExceptionHandler will automatically be applied to the method body after your manipulator method is finished.

C#
public ILWeaver HandlerCreateFinally(out WeaverExceptionFinallyHandler handler)

handler WeaverExceptionFinallyHandler
The created IWeaverExceptionHandler to be configured.

ILWeaver
The new exception handler instance.

See WeaverExceptionFinallyHandler for more information about this handler type.

HandlerCreateFault(out WeaverExceptionFaultHandler)

Section titled “HandlerCreateFault(out WeaverExceptionFaultHandler)”

Create a new IWeaverExceptionHandler to be assigned its ranges. Note that certain values can be figured out implicitly such as HandlerStart when TryEnd is defined.

The IWeaverExceptionHandler will automatically be applied to the method body after your manipulator method is finished.

C#
public ILWeaver HandlerCreateFault(out WeaverExceptionFaultHandler handler)

handler WeaverExceptionFaultHandler
The created IWeaverExceptionHandler to be configured.

ILWeaver
The new exception handler instance.

See WeaverExceptionFaultHandler for more information about this handler type.

HandlerSetTryStart(ILLabel, IWeaverExceptionHandler)

Section titled “HandlerSetTryStart(ILLabel, IWeaverExceptionHandler)”

Set the TryStart property of the IWeaverExceptionHandler.

C#
public ILWeaver HandlerSetTryStart(ILLabel tryStart, IWeaverExceptionHandler handler)

tryStart MonoMod.Cil.ILLabel
The first ILLabel in the try block.

handler IWeaverExceptionHandler
The IWeaverExceptionHandler to configure.

ILWeaver
This ILWeaver.

This range is inclusive.

HandlerSetTryStart(Instruction, IWeaverExceptionHandler)

Section titled “HandlerSetTryStart(Instruction, IWeaverExceptionHandler)”

Set the TryStart property of the IWeaverExceptionHandler.

C#
public ILWeaver HandlerSetTryStart(Instruction tryStart, IWeaverExceptionHandler handler)

tryStart Instruction
The first ILLabel in the try block.

handler IWeaverExceptionHandler
The IWeaverExceptionHandler to configure.

ILWeaver
This ILWeaver.

This range is inclusive.

HandlerSetTryEnd(ILLabel, IWeaverExceptionHandler)

Section titled “HandlerSetTryEnd(ILLabel, IWeaverExceptionHandler)”

Set the TryEnd property of the IWeaverExceptionHandler.

C#
public ILWeaver HandlerSetTryEnd(ILLabel tryEnd, IWeaverExceptionHandler handler)

tryEnd MonoMod.Cil.ILLabel
The last instruction in the try block.

handler IWeaverExceptionHandler
The IWeaverExceptionHandler to configure.

ILWeaver
This ILWeaver.

This range is inclusive.

HandlerSetTryEnd(Instruction, IWeaverExceptionHandler)

Section titled “HandlerSetTryEnd(Instruction, IWeaverExceptionHandler)”

Set the TryEnd property of the IWeaverExceptionHandler.

C#
public ILWeaver HandlerSetTryEnd(Instruction tryEnd, IWeaverExceptionHandler handler)

tryEnd Instruction
The last instruction in the try block.

handler IWeaverExceptionHandler
The IWeaverExceptionHandler to configure.

ILWeaver
This ILWeaver.

This range is inclusive.

HandlerSetFilterStart(ILLabel, WeaverExceptionFilterHandler)

Section titled “HandlerSetFilterStart(ILLabel, WeaverExceptionFilterHandler)”

Set the FilterStart property of the WeaverExceptionFilterHandler.

C#
public ILWeaver HandlerSetFilterStart(ILLabel filterStart, WeaverExceptionFilterHandler handler)

filterStart MonoMod.Cil.ILLabel
The first ILLabel in the filter block.

handler WeaverExceptionFilterHandler
The IWeaverExceptionHandler to configure.

ILWeaver
This ILWeaver.

This range is inclusive.

HandlerSetFilterStart(Instruction, WeaverExceptionFilterHandler)

Section titled “HandlerSetFilterStart(Instruction, WeaverExceptionFilterHandler)”

Set the FilterStart property of the WeaverExceptionFilterHandler.

C#
public ILWeaver HandlerSetFilterStart(Instruction filterStart, WeaverExceptionFilterHandler handler)

filterStart Instruction
The first ILLabel in the filter block.

handler WeaverExceptionFilterHandler
The IWeaverExceptionHandler to configure.

ILWeaver
This ILWeaver.

This range is inclusive.

HandlerSetHandlerStart(ILLabel, IWeaverExceptionHandler)

Section titled “HandlerSetHandlerStart(ILLabel, IWeaverExceptionHandler)”

Set the HandlerStart property of the IWeaverExceptionHandler.

C#
public ILWeaver HandlerSetHandlerStart(ILLabel handlerStart, IWeaverExceptionHandler handler)

handlerStart MonoMod.Cil.ILLabel
The first ILLabel in the catch block.

handler IWeaverExceptionHandler
The IWeaverExceptionHandler to configure.

ILWeaver
This ILWeaver.

This range is inclusive.

HandlerSetHandlerStart(Instruction, IWeaverExceptionHandler)

Section titled “HandlerSetHandlerStart(Instruction, IWeaverExceptionHandler)”

Set the HandlerStart property of the IWeaverExceptionHandler.

C#
public ILWeaver HandlerSetHandlerStart(Instruction handlerStart, IWeaverExceptionHandler handler)

handlerStart Instruction
The first ILLabel in the catch block.

handler IWeaverExceptionHandler
The IWeaverExceptionHandler to configure.

ILWeaver
This ILWeaver.

This range is inclusive.

HandlerSetHandlerEnd(ILLabel, IWeaverExceptionHandler)

Section titled “HandlerSetHandlerEnd(ILLabel, IWeaverExceptionHandler)”

Set the HandlerEnd property of the IWeaverExceptionHandler.

C#
public ILWeaver HandlerSetHandlerEnd(ILLabel handlerEnd, IWeaverExceptionHandler handler)

handlerEnd MonoMod.Cil.ILLabel
The last ILLabel in the catch block.

handler IWeaverExceptionHandler
The IWeaverExceptionHandler to configure.

ILWeaver
This ILWeaver.

This range is inclusive.

HandlerSetHandlerEnd(Instruction, IWeaverExceptionHandler)

Section titled “HandlerSetHandlerEnd(Instruction, IWeaverExceptionHandler)”

Set the HandlerEnd property of the IWeaverExceptionHandler.

C#
public ILWeaver HandlerSetHandlerEnd(Instruction handlerEnd, IWeaverExceptionHandler handler)

handlerEnd Instruction
The last ILLabel in the catch block.

handler IWeaverExceptionHandler
The IWeaverExceptionHandler to configure.

ILWeaver
This ILWeaver.

This range is inclusive.

HandlerWrapTryCatchStackSizeNonZeroOnCurrent(Type?, params IEnumerable<Instruction>)

Section titled “HandlerWrapTryCatchStackSizeNonZeroOnCurrent(Type?, params IEnumerable<Instruction>)”

Wraps an area around Current where the stack size is non-zero to a try block, catching exceptions of type catchType. The argument catchInstructions is written as the catch block’s instructions.

After this method, Current will be on the instruction after the catch block.

C#
public ILWeaver HandlerWrapTryCatchStackSizeNonZeroOnCurrent(Type? catchType, params IEnumerable<Instruction> catchInstructions)

catchType Type
The types of Exceptions that should be catched. If left null, Exception is used.

catchInstructions IEnumerable<Instruction>
The instructions to be written to the catch handler.

ILWeaver
This ILWeaver.

HandlerWrapTryCatchStackSizeNonZeroOnCurrent(Type?, params IEnumerable<InstructionOrEnumerable>)

Section titled “HandlerWrapTryCatchStackSizeNonZeroOnCurrent(Type?, params IEnumerable<InstructionOrEnumerable>)”

Wraps an area around Current where the stack size is non-zero to a try block, catching exceptions of type catchType. The argument catchInstructions is written as the catch block’s instructions.

After this method, Current will be on the instruction after the catch block.

C#
public ILWeaver HandlerWrapTryCatchStackSizeNonZeroOnCurrent(Type? catchType, params IEnumerable<InstructionOrEnumerable> catchInstructions)

catchType Type
The types of Exceptions that should be catched. If left null, Exception is used.

catchInstructions IEnumerable<InstructionOrEnumerable>
The instructions to be written to the catch handler.

ILWeaver
This ILWeaver.

HandlerWrapTryCatchStackSizeNonZeroOnCurrent(Type?, Action)

Section titled “HandlerWrapTryCatchStackSizeNonZeroOnCurrent(Type?, Action)”

Wraps an area around Current where the stack size is non-zero to a try block, catching exceptions of type catchType. Catching logic can be written in the writeCatch callback, where at the start Current is the inclusive end of the try block, and at the end Current should be the end of the catch block.

After this method, Current will be on the instruction after the catch block.

C#
public ILWeaver HandlerWrapTryCatchStackSizeNonZeroOnCurrent(Type? catchType, Action writeCatch)

catchType Type
The types of Exceptions that should be catched. If left null, Exception is used.

writeCatch Action
The callback where catch logic is written.

ILWeaver
This ILWeaver.

HandlerWrapTryCatchStackSizeNonZero(Type?, Instruction, out Instruction, params IEnumerable<Instruction>)

Section titled “HandlerWrapTryCatchStackSizeNonZero(Type?, Instruction, out Instruction, params IEnumerable<Instruction>)”

Wraps an area around origin where the stack size is non-zero to a try block, catching exceptions of type catchType. The argument catchInstructions is written as the catch block’s instructions.

C#
public ILWeaver HandlerWrapTryCatchStackSizeNonZero(Type? catchType, Instruction origin, out Instruction afterCatch, params IEnumerable<Instruction> catchInstructions)

catchType Type
The types of Exceptions that should be catched. If left null, Exception is used.

origin Instruction
The instruction around which the area where stack size is non-zero is selected.

afterCatch Instruction
The instruction outside the written catch handler.

catchInstructions IEnumerable<Instruction>
The instructions to be written to the catch handler.

ILWeaver
This ILWeaver.

HandlerWrapTryCatchStackSizeNonZero(Type?, Instruction, out Instruction, params IEnumerable<InstructionOrEnumerable>)

Section titled “HandlerWrapTryCatchStackSizeNonZero(Type?, Instruction, out Instruction, params IEnumerable<InstructionOrEnumerable>)”

Wraps an area around origin where the stack size is non-zero to a try block, catching exceptions of type catchType. The argument catchInstructions is written as the catch block’s instructions.

C#
public ILWeaver HandlerWrapTryCatchStackSizeNonZero(Type? catchType, Instruction origin, out Instruction afterCatch, params IEnumerable<InstructionOrEnumerable> catchInstructions)

catchType Type
The types of Exceptions that should be catched. If left null, Exception is used.

origin Instruction
The instruction around which the area where stack size is non-zero is selected.

afterCatch Instruction
The instruction outside the written catch handler.

catchInstructions IEnumerable<InstructionOrEnumerable>
The instructions to be written to the catch handler.

ILWeaver
This ILWeaver.

HandlerWrapTryCatchStackSizeNonZero(Type?, Instruction, out Instruction, Func<Instruction, Instruction>)

Section titled “HandlerWrapTryCatchStackSizeNonZero(Type?, Instruction, out Instruction, Func<Instruction, Instruction>)”

Wraps an area around origin where the stack size is non-zero to a try block, catching exceptions of type catchType. Catching logic can be written in the writeCatch callback, where the Instruction parameter is the inclusive end of the try block, and the return value is the end of the catch block.

C#
public ILWeaver HandlerWrapTryCatchStackSizeNonZero(Type? catchType, Instruction origin, out Instruction afterCatch, Func<Instruction, Instruction> writeCatch)

catchType Type
The types of Exceptions that should be catched. If left null, Exception is used.

origin Instruction
The instruction around which the area where stack size is non-zero is selected.

afterCatch Instruction
The instruction outside the written catch handler.

writeCatch Func<Instruction, Instruction>
The callback where catch logic is written.

ILWeaver
This ILWeaver.

Note: This method is automatically called for all unapplied handlers after your manipulator method is finished. This wasn’t always the case, but it’s unlikely you’ll ever need to call this anymore.

Writes the leave instructions for try, catch or finally blocks and applies the provided IWeaverExceptionHandler to the method body.

C#
public ILWeaver HandlerApply(IWeaverExceptionHandler handler)

handler IWeaverExceptionHandler
The handler to apply.

ILWeaver
This ILWeaver.

NullReferenceException

Once applied, the leave label of the handler leave instructions will point to the instruction that comes after what was set as HandlerEnd. Make sure that once you have applied the IWeaverExceptionHandler, you are not inserting instructions before the HandlerEnd or you’ll need to retarget the leave label to your first inserted instruction before the EndHandler.

Replaces the target instruction.

C#
public ILWeaver Replace(Instruction target, Instruction replacement)

target Instruction
The instruction to replace.

replacement Instruction
The replacement instruction.

ILWeaver
this ILWeaver.

The target instruction instance is left untouched and instead the actual instance of the instruction that replaces it takes its place, and as such it steals all the labels of target to itself.

If Current points to the instruction being replaced, it is moved to the replacement instruction.

Replace(Instruction, params IEnumerable<Instruction>)

Section titled “Replace(Instruction, params IEnumerable<Instruction>)”

Replaces the target instruction with the first instruction of the replacement IEnumerable and inserts the rest of the instructions after.

C#
public ILWeaver Replace(Instruction target, params IEnumerable<Instruction> replacement)

target Instruction
The instruction to replace.

replacement IEnumerable<Instruction>
The replacement instructions. The first instruction replaces the target.

ILWeaver
this ILWeaver.

The target instruction instance is left untouched and instead the actual instance of the instruction that replaces it takes its place, and as such it steals all the labels of target to itself.

If Current points to the instruction being replaced, it is moved to the replacement instruction.

Replace(Instruction, params IEnumerable<InstructionOrEnumerable>)

Section titled “Replace(Instruction, params IEnumerable<InstructionOrEnumerable>)”

Replaces the target instruction with the first instruction of the replacement IEnumerable and inserts the rest of the instructions after.

C#
public ILWeaver Replace(Instruction target, params IEnumerable<InstructionOrEnumerable> replacement)

target Instruction
The instruction to replace.

replacement IEnumerable<InstructionOrEnumerable>
The replacement instructions. The first instruction replaces the target.

ILWeaver
this ILWeaver.

The target instruction instance is left untouched and instead the actual instance of the instruction that replaces it takes its place, and as such it steals all the labels of target to itself.

If Current points to the instruction being replaced, it is moved to the replacement instruction.

Replaces the instruction at Current.

C#
public ILWeaver ReplaceCurrent(Instruction replacement)

replacement Instruction
The replacement instruction.

ILWeaver
this ILWeaver.

The Current instruction instance is left untouched and instead the actual instance of the instruction that replaces it takes its place, and as such it steals all the labels of Current to itself.

ReplaceCurrent(params IEnumerable<Instruction>)

Section titled “ReplaceCurrent(params IEnumerable<Instruction>)”

Replaces the Current instruction with the first instruction of the replacement IEnumerable and inserts the rest of the instructions after.

C#
public ILWeaver ReplaceCurrent(params IEnumerable<Instruction> replacement)

replacement IEnumerable<Instruction>
The replacement instructions. The first instruction replaces the Current.

ILWeaver
this ILWeaver.

The Current instruction instance is left untouched and instead the actual instance of the instruction that replaces it takes its place, and as such it steals all the labels of Current to itself.

ReplaceCurrent(params IEnumerable<InstructionOrEnumerable>)

Section titled “ReplaceCurrent(params IEnumerable<InstructionOrEnumerable>)”

Replaces the Current instruction with the first instruction of the replacement IEnumerable and inserts the rest of the instructions after.

C#
public ILWeaver ReplaceCurrent(params IEnumerable<InstructionOrEnumerable> replacement)

replacement IEnumerable<InstructionOrEnumerable>
The replacement instructions. The first instruction replaces the Current.

ILWeaver
this ILWeaver.

The Current instruction instance is left untouched and instead the actual instance of the instruction that replaces it takes its place, and as such it steals all the labels of Current to itself.

Replaces the target instruction’s Operand.

C#
public ILWeaver ReplaceOperand(Instruction target, object replacementOperand)

target Instruction
The instruction whose Operand to replace.

replacementOperand object
The new operand value to replace the old one.

ILWeaver
this ILWeaver.

The target instruction instance is left untouched and instead a copy of the instruction with the replacement operand takes its place, and as such it steals all the labels of target to itself.

If Current points to the instruction being replaced, it is moved to the replacement instruction.

Replaces the Current instruction’s Operand.

C#
public ILWeaver ReplaceCurrentOperand(object replacementOperand)

replacementOperand object
The new operand value to replace the old one.

ILWeaver
this ILWeaver.

The Current instruction instance is left untouched and instead a copy of the instruction with the replacement operand takes its place, and as such it steals all the labels of Current to itself.

Removes the provided instruction from the method body and moves all its labels and exception handler range roles to the next instruction.

Important: If you are removing an instruction to replace it, use Replace(Instruction, Instruction) or any of the variants instead.

C#
public ILWeaver RemoveAndShiftLabels(Instruction instruction)

instruction Instruction
The instruction to remove.

ILWeaver
This ILWeaver.

If there is no next instruction, the previous instruction will be the shift target. If there is no previous instruction either, the next inserted instruction will be the new shift target.

If Current points to an instruction to be removed, it is moved to the next instruction alongside the labels.

Note: Removing instructions have consequences as described by this method. This method does what is necessary to not break the target method in cases where labels would not be shifted. As such, there is no variant which does not shift labels (that is not deprecate anyways).

Removes Current from the method body and moves all its labels and exception handler range roles to the next instruction.

Important: If you are removing an instruction to replace it, use ReplaceCurrent(Instruction) or any of the variants instead.

C#
public ILWeaver RemoveCurrentAndShiftLabels()

ILWeaver
This ILWeaver.

If there is no next instruction, the previous instruction will be the shift target. If there is no previous instruction either, the next inserted instruction will be the new shift target.

If Current points to an instruction to be removed, it is moved to the next instruction alongside the labels.

Note: Removing instructions have consequences as described by this method. This method does what is necessary to not break the target method in cases where labels would not be shifted. As such, there is no variant which does not shift labels (that is not deprecate anyways).

RemoveRangeAndShiftLabels(Instruction, Instruction)

Section titled “RemoveRangeAndShiftLabels(Instruction, Instruction)”

Removes the instructions in the inclusive start to end range from the method body and moves all their labels and exception handler range roles to the next available instruction.

C#
public ILWeaver RemoveRangeAndShiftLabels(Instruction start, Instruction end)

start Instruction
The first instruction in the range to remove.

end Instruction
The last instruction in the range to remove.

ILWeaver
This ILWeaver.

The order of start and end does not matter.

If there is no next instruction, the previous instruction will be the shift target. If there is no previous instruction either, the next inserted instruction will be the new shift target.

If Current points to an instruction to be removed, it is moved to the next available instruction alongside the labels.

Exception handlers: If the range contains the HandlerStart and previous instruction of HandlerEnd of an exception handler in a single RemoveRangeAndShiftLabels(Instruction, Instruction) call, the exception handler is removed from the method body as leaving it be would cause invalid IL. If you wish to rewrite the handler block instead, don’t remove the previous instruction of the leave target which is HandlerEnd.

Note: Removing instructions have consequences as described by this method. This method does what is necessary to not break the target method in cases where labels would not be shifted. As such, there is no variant which does not shift labels (that is not deprecate anyways).

InsertBeforeStealLabels(int, params IEnumerable<Instruction>)

Section titled “InsertBeforeStealLabels(int, params IEnumerable<Instruction>)”

Insert instructions before the provided index, stealing any labels.

C#
public ILWeaver InsertBeforeStealLabels(int index, params IEnumerable<Instruction> instructions)

index int

instructions IEnumerable<Instruction>

ILWeaver

Stealing labels means that if the instruction at the provided instruction or index has incoming MonoMod.Cil.ILLabels or is inside the start of a try, filter, catch, finally, or fault range, then the first inserted instruction will become the new start of that range or label. The same applies to the ends of these handler ranges because they are exclusive, meaning that the inclusive end of a range is before the instruction marked as the end.

InsertBeforeStealLabels(int, params IEnumerable<InstructionOrEnumerable>)

Section titled “InsertBeforeStealLabels(int, params IEnumerable<InstructionOrEnumerable>)”

Insert instructions before the provided index, stealing any labels.

C#
public ILWeaver InsertBeforeStealLabels(int index, params IEnumerable<InstructionOrEnumerable> instructions)

index int

instructions IEnumerable<InstructionOrEnumerable>

ILWeaver

Stealing labels means that if the instruction at the provided instruction or index has incoming MonoMod.Cil.ILLabels or is inside the start of a try, filter, catch, finally, or fault range, then the first inserted instruction will become the new start of that range or label. The same applies to the ends of these handler ranges because they are exclusive, meaning that the inclusive end of a range is before the instruction marked as the end.

InsertBeforeStealLabels(Instruction, params IEnumerable<Instruction>)

Section titled “InsertBeforeStealLabels(Instruction, params IEnumerable<Instruction>)”

Insert instructions before the provided instruction, stealing any labels.

C#
public ILWeaver InsertBeforeStealLabels(Instruction target, params IEnumerable<Instruction> instructions)

target Instruction

instructions IEnumerable<Instruction>

ILWeaver

Stealing labels means that if the instruction at the provided instruction or index has incoming MonoMod.Cil.ILLabels or is inside the start of a try, filter, catch, finally, or fault range, then the first inserted instruction will become the new start of that range or label. The same applies to the ends of these handler ranges because they are exclusive, meaning that the inclusive end of a range is before the instruction marked as the end.

InsertBeforeStealLabels(Instruction, params IEnumerable<InstructionOrEnumerable>)

Section titled “InsertBeforeStealLabels(Instruction, params IEnumerable<InstructionOrEnumerable>)”

Insert instructions before the provided instruction, stealing any labels.

C#
public ILWeaver InsertBeforeStealLabels(Instruction target, params IEnumerable<InstructionOrEnumerable> instructions)

target Instruction

instructions IEnumerable<InstructionOrEnumerable>

ILWeaver

Stealing labels means that if the instruction at the provided instruction or index has incoming MonoMod.Cil.ILLabels or is inside the start of a try, filter, catch, finally, or fault range, then the first inserted instruction will become the new start of that range or label. The same applies to the ends of these handler ranges because they are exclusive, meaning that the inclusive end of a range is before the instruction marked as the end.

InsertBeforeCurrentStealLabels(params IEnumerable<Instruction>)

Section titled “InsertBeforeCurrentStealLabels(params IEnumerable<Instruction>)”

Insert instructions before this weaver’s current position, stealing any labels. Current target doesn’t change.

C#
public ILWeaver InsertBeforeCurrentStealLabels(params IEnumerable<Instruction> instructions)

instructions IEnumerable<Instruction>

ILWeaver

Stealing labels means that if the instruction at the provided instruction or index has incoming MonoMod.Cil.ILLabels or is inside the start of a try, filter, catch, finally, or fault range, then the first inserted instruction will become the new start of that range or label. The same applies to the ends of these handler ranges because they are exclusive, meaning that the inclusive end of a range is before the instruction marked as the end.

InsertBeforeCurrentStealLabels(params IEnumerable<InstructionOrEnumerable>)

Section titled “InsertBeforeCurrentStealLabels(params IEnumerable<InstructionOrEnumerable>)”

Insert instructions before this weaver’s current position, stealing any labels. Current target doesn’t change.

C#
public ILWeaver InsertBeforeCurrentStealLabels(params IEnumerable<InstructionOrEnumerable> instructions)

instructions IEnumerable<InstructionOrEnumerable>

ILWeaver

Stealing labels means that if the instruction at the provided instruction or index has incoming MonoMod.Cil.ILLabels or is inside the start of a try, filter, catch, finally, or fault range, then the first inserted instruction will become the new start of that range or label. The same applies to the ends of these handler ranges because they are exclusive, meaning that the inclusive end of a range is before the instruction marked as the end.

InsertBefore(int, params IEnumerable<Instruction>)

Section titled “InsertBefore(int, params IEnumerable<Instruction>)”

Insert instructions before the provided index.

C#
public ILWeaver InsertBefore(int index, params IEnumerable<Instruction> instructions)

index int

instructions IEnumerable<Instruction>

ILWeaver

InsertBefore(int, params IEnumerable<InstructionOrEnumerable>)

Section titled “InsertBefore(int, params IEnumerable<InstructionOrEnumerable>)”

Insert instructions before the provided index.

C#
public ILWeaver InsertBefore(int index, params IEnumerable<InstructionOrEnumerable> instructions)

index int

instructions IEnumerable<InstructionOrEnumerable>

ILWeaver

InsertBefore(Instruction, params IEnumerable<Instruction>)

Section titled “InsertBefore(Instruction, params IEnumerable<Instruction>)”

Insert instructions before the provided instruction.

C#
public ILWeaver InsertBefore(Instruction target, params IEnumerable<Instruction> instructions)

target Instruction

instructions IEnumerable<Instruction>

ILWeaver

InsertBefore(Instruction, params IEnumerable<InstructionOrEnumerable>)

Section titled “InsertBefore(Instruction, params IEnumerable<InstructionOrEnumerable>)”

Insert instructions before the provided instruction.

C#
public ILWeaver InsertBefore(Instruction target, params IEnumerable<InstructionOrEnumerable> instructions)

target Instruction

instructions IEnumerable<InstructionOrEnumerable>

ILWeaver

InsertBeforeCurrent(params IEnumerable<Instruction>)

Section titled “InsertBeforeCurrent(params IEnumerable<Instruction>)”

Insert instructions before this weaver’s current position.

C#
public ILWeaver InsertBeforeCurrent(params IEnumerable<Instruction> instructions)

instructions IEnumerable<Instruction>

ILWeaver

InsertBeforeCurrent(params IEnumerable<InstructionOrEnumerable>)

Section titled “InsertBeforeCurrent(params IEnumerable<InstructionOrEnumerable>)”

Insert instructions before this weaver’s current position.

C#
public ILWeaver InsertBeforeCurrent(params IEnumerable<InstructionOrEnumerable> instructions)

instructions IEnumerable<InstructionOrEnumerable>

ILWeaver

InsertAfter(int, params IEnumerable<Instruction>)

Section titled “InsertAfter(int, params IEnumerable<Instruction>)”

Insert instructions after the provided index.

C#
public ILWeaver InsertAfter(int index, params IEnumerable<Instruction> instructions)

index int

instructions IEnumerable<Instruction>

ILWeaver

InsertAfter(int, params IEnumerable<InstructionOrEnumerable>)

Section titled “InsertAfter(int, params IEnumerable<InstructionOrEnumerable>)”

Insert instructions after the provided index.

C#
public ILWeaver InsertAfter(int index, params IEnumerable<InstructionOrEnumerable> instructions)

index int

instructions IEnumerable<InstructionOrEnumerable>

ILWeaver

InsertAfter(Instruction, params IEnumerable<Instruction>)

Section titled “InsertAfter(Instruction, params IEnumerable<Instruction>)”

Insert instructions after the provided instruction.

C#
public ILWeaver InsertAfter(Instruction target, params IEnumerable<Instruction> instructions)

target Instruction

instructions IEnumerable<Instruction>

ILWeaver

InsertAfter(Instruction, params IEnumerable<InstructionOrEnumerable>)

Section titled “InsertAfter(Instruction, params IEnumerable<InstructionOrEnumerable>)”

Insert instructions after the provided instruction.

C#
public ILWeaver InsertAfter(Instruction target, params IEnumerable<InstructionOrEnumerable> instructions)

target Instruction

instructions IEnumerable<InstructionOrEnumerable>

ILWeaver

InsertAfterCurrent(params IEnumerable<Instruction>)

Section titled “InsertAfterCurrent(params IEnumerable<Instruction>)”

Insert instructions after this weaver’s current position. Retargets Current to the last inserted instruction.

C#
public ILWeaver InsertAfterCurrent(params IEnumerable<Instruction> instructions)

instructions IEnumerable<Instruction>

ILWeaver

InsertAfterCurrent(params IEnumerable<InstructionOrEnumerable>)

Section titled “InsertAfterCurrent(params IEnumerable<InstructionOrEnumerable>)”

Insert instructions after this weaver’s current position. Retargets Current to the last inserted instruction.

C#
public ILWeaver InsertAfterCurrent(params IEnumerable<InstructionOrEnumerable> instructions)

instructions IEnumerable<InstructionOrEnumerable>

ILWeaver

InsertBranchOverIfTrue(Instruction, Instruction, params IEnumerable<Instruction>)

Section titled “InsertBranchOverIfTrue(Instruction, Instruction, params IEnumerable<Instruction>)”

Inserts a brtrue instruction before start instruction which, if the provided condition returns true (non-zero on the stack), branches OVER the end instruction, thus the instruction after “end” is branched to.

C#
public ILWeaver InsertBranchOverIfTrue(Instruction start, Instruction end, params IEnumerable<Instruction> condition)

start Instruction
The first instruction to branch over.

end Instruction
The last instruction to branch over.

condition IEnumerable<Instruction>
A list of instructions which end up leaving one (1) value on the stack which represents either true (non-zero value, e.g. ldc.i4.1) or false (zero e.g. ldc.i4.0) which will be evaluated by brtrue.

ILWeaver
This ILWeaver.

If control flow would fall or branch to start instruction, the inserted instruction runs. However if the start instruction is skipped in control flow, the inserted instruction is also skipped. This means instructions after start and up to end are possible to execute if they are directly branched to.

InsertBranchOverIfTrue(Instruction, Instruction, params IEnumerable<InstructionOrEnumerable>)

Section titled “InsertBranchOverIfTrue(Instruction, Instruction, params IEnumerable<InstructionOrEnumerable>)”

Inserts a brtrue instruction before start instruction which, if the provided condition returns true (non-zero on the stack), branches OVER the end instruction, thus the instruction after “end” is branched to.

C#
public ILWeaver InsertBranchOverIfTrue(Instruction start, Instruction end, params IEnumerable<InstructionOrEnumerable> condition)

start Instruction
The first instruction to branch over.

end Instruction
The last instruction to branch over.

condition IEnumerable<InstructionOrEnumerable>
A list of instructions which end up leaving one (1) value on the stack which represents either true (non-zero value, e.g. ldc.i4.1) or false (zero e.g. ldc.i4.0) which will be evaluated by brtrue.

ILWeaver
This ILWeaver.

If control flow would fall or branch to start instruction, the inserted instruction runs. However if the start instruction is skipped in control flow, the inserted instruction is also skipped. This means instructions after start and up to end are possible to execute if they are directly branched to.

InsertBranchOverIfTrue((Instruction start, Instruction end), params IEnumerable<Instruction>)

Section titled “InsertBranchOverIfTrue((Instruction start, Instruction end), params IEnumerable<Instruction>)”

Inserts a brtrue instruction before start instruction which, if the provided condition returns true (non-zero on the stack), branches OVER the end instruction, thus the instruction after “end” is branched to.

C#
public ILWeaver InsertBranchOverIfTrue((Instruction start, Instruction end) range, params IEnumerable<Instruction> condition)

range (Instruction start, Instruction end)
A tuple of the first and last instructions to branch over.

condition IEnumerable<Instruction>
A list of instructions which end up leaving one (1) value on the stack which represents either true (non-zero value, e.g. ldc.i4.1) or false (zero e.g. ldc.i4.0) which will be evaluated by brtrue.

ILWeaver
This ILWeaver.

If control flow would fall or branch to start instruction, the inserted instruction runs. However if the start instruction is skipped in control flow, the inserted instruction is also skipped. This means instructions after start and up to end are possible to execute if they are directly branched to.

InsertBranchOverIfTrue((Instruction start, Instruction end), params IEnumerable<InstructionOrEnumerable>)

Section titled “InsertBranchOverIfTrue((Instruction start, Instruction end), params IEnumerable<InstructionOrEnumerable>)”

Inserts a brtrue instruction before start instruction which, if the provided condition returns true (non-zero on the stack), branches OVER the end instruction, thus the instruction after “end” is branched to.

C#
public ILWeaver InsertBranchOverIfTrue((Instruction start, Instruction end) range, params IEnumerable<InstructionOrEnumerable> condition)

range (Instruction start, Instruction end)
A tuple of the first and last instructions to branch over.

condition IEnumerable<InstructionOrEnumerable>
A list of instructions which end up leaving one (1) value on the stack which represents either true (non-zero value, e.g. ldc.i4.1) or false (zero e.g. ldc.i4.0) which will be evaluated by brtrue.

ILWeaver
This ILWeaver.

If control flow would fall or branch to start instruction, the inserted instruction runs. However if the start instruction is skipped in control flow, the inserted instruction is also skipped. This means instructions after start and up to end are possible to execute if they are directly branched to.

InsertBranchOverIfFalse(Instruction, Instruction, params IEnumerable<Instruction>)

Section titled “InsertBranchOverIfFalse(Instruction, Instruction, params IEnumerable<Instruction>)”

Inserts a brfalse instruction before start instruction which, if the provided condition returns false (zero on the stack), branches OVER the end instruction, thus the instruction after “end” is branched to.

C#
public ILWeaver InsertBranchOverIfFalse(Instruction start, Instruction end, params IEnumerable<Instruction> condition)

start Instruction
The first instruction to branch over.

end Instruction
The last instruction to branch over.

condition IEnumerable<Instruction>
A list of instructions which end up leaving one (1) value on the stack which represents either true (non-zero value, e.g. ldc.i4.1) or false (zero e.g. ldc.i4.0) which will be evaluated by brfalse.

ILWeaver
This ILWeaver.

If control flow would fall or branch to start instruction, the inserted instruction runs. However if the start instruction is skipped in control flow, the inserted instruction is also skipped. This means instructions after start and up to end are possible to execute if they are directly branched to.

InsertBranchOverIfFalse(Instruction, Instruction, params IEnumerable<InstructionOrEnumerable>)

Section titled “InsertBranchOverIfFalse(Instruction, Instruction, params IEnumerable<InstructionOrEnumerable>)”

Inserts a brfalse instruction before start instruction which, if the provided condition returns false (zero on the stack), branches OVER the end instruction, thus the instruction after “end” is branched to.

C#
public ILWeaver InsertBranchOverIfFalse(Instruction start, Instruction end, params IEnumerable<InstructionOrEnumerable> condition)

start Instruction
The first instruction to branch over.

end Instruction
The last instruction to branch over.

condition IEnumerable<InstructionOrEnumerable>
A list of instructions which end up leaving one (1) value on the stack which represents either true (non-zero value, e.g. ldc.i4.1) or false (zero e.g. ldc.i4.0) which will be evaluated by brfalse.

ILWeaver
This ILWeaver.

If control flow would fall or branch to start instruction, the inserted instruction runs. However if the start instruction is skipped in control flow, the inserted instruction is also skipped. This means instructions after start and up to end are possible to execute if they are directly branched to.

InsertBranchOverIfFalse((Instruction start, Instruction end), params IEnumerable<Instruction>)

Section titled “InsertBranchOverIfFalse((Instruction start, Instruction end), params IEnumerable<Instruction>)”

Inserts a brfalse instruction before start instruction which, if the provided condition returns false (zero on the stack), branches OVER the end instruction, thus the instruction after “end” is branched to.

C#
public ILWeaver InsertBranchOverIfFalse((Instruction start, Instruction end) range, params IEnumerable<Instruction> condition)

range (Instruction start, Instruction end)
A tuple of the first and last instructions to branch over.

condition IEnumerable<Instruction>
A list of instructions which end up leaving one (1) value on the stack which represents either true (non-zero value, e.g. ldc.i4.1) or false (zero e.g. ldc.i4.0) which will be evaluated by brfalse.

ILWeaver
This ILWeaver.

If control flow would fall or branch to start instruction, the inserted instruction runs. However if the start instruction is skipped in control flow, the inserted instruction is also skipped. This means instructions after start and up to end are possible to execute if they are directly branched to.

InsertBranchOverIfFalse((Instruction start, Instruction end), params IEnumerable<InstructionOrEnumerable>)

Section titled “InsertBranchOverIfFalse((Instruction start, Instruction end), params IEnumerable<InstructionOrEnumerable>)”

Inserts a brfalse instruction before start instruction which, if the provided condition returns false (zero on the stack), branches OVER the end instruction, thus the instruction after “end” is branched to.

C#
public ILWeaver InsertBranchOverIfFalse((Instruction start, Instruction end) range, params IEnumerable<InstructionOrEnumerable> condition)

range (Instruction start, Instruction end)
A tuple of the first and last instructions to branch over.

condition IEnumerable<InstructionOrEnumerable>
A list of instructions which end up leaving one (1) value on the stack which represents either true (non-zero value, e.g. ldc.i4.1) or false (zero e.g. ldc.i4.0) which will be evaluated by brfalse.

ILWeaver
This ILWeaver.

If control flow would fall or branch to start instruction, the inserted instruction runs. However if the start instruction is skipped in control flow, the inserted instruction is also skipped. This means instructions after start and up to end are possible to execute if they are directly branched to.

InsertBranchOver(Instruction, Instruction)

Section titled “InsertBranchOver(Instruction, Instruction)”

Inserts a br instruction before start instruction which unconditionally branches OVER the end instruction, thus the instruction after “end” is branched to.

C#
public ILWeaver InsertBranchOver(Instruction start, Instruction end)

start Instruction
The first instruction to branch over.

end Instruction
The last instruction to branch over.

ILWeaver
This ILWeaver.

If control flow would fall or branch to start instruction, the inserted instruction runs. However if the start instruction is skipped in control flow, the inserted instruction is also skipped. This means instructions after start and up to end are possible to execute if they are directly branched to.

InsertBranchOver((Instruction start, Instruction end))

Section titled “InsertBranchOver((Instruction start, Instruction end))”

Inserts a br instruction before start instruction which unconditionally branches OVER the end instruction, thus the instruction after “end” is branched to.

C#
public ILWeaver InsertBranchOver((Instruction start, Instruction end) range)

range (Instruction start, Instruction end)
A tuple of the first and last instructions to branch over.

ILWeaver
This ILWeaver.

If control flow would fall or branch to start instruction, the inserted instruction runs. However if the start instruction is skipped in control flow, the inserted instruction is also skipped. This means instructions after start and up to end are possible to execute if they are directly branched to.

EmitReferenceBefore<T>(Instruction, in T?, out int)

Section titled “EmitReferenceBefore<T>(Instruction, in T?, out int)”

Store an object in the reference store, and emit the IL to retrieve it and place it on the stack.

C#
public ILWeaver EmitReferenceBefore<T>(Instruction target, in T? value, out int id)

target Instruction

value T

id int

ILWeaver

EmitReferenceBeforeCurrent<T>(in T?, out int)

Section titled “EmitReferenceBeforeCurrent<T>(in T?, out int)”

Store an object in the reference store, and emit the IL to retrieve it and place it on the stack.

C#
public ILWeaver EmitReferenceBeforeCurrent<T>(in T? value, out int id)

value T

id int

ILWeaver

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, ParameterDefinition parameter)

opcode OpCode
The instruction opcode.

parameter ParameterDefinition
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, VariableDefinition variable)

opcode OpCode
The instruction opcode.

variable VariableDefinition
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, Instruction[] targets)

opcode OpCode
The instruction opcode.

targets Instruction[]
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, Instruction target)

opcode OpCode
The instruction opcode.

target Instruction
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, double value)

opcode OpCode
The instruction opcode.

value double
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, float value)

opcode OpCode
The instruction opcode.

value float
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, long value)

opcode OpCode
The instruction opcode.

value long
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, sbyte value)

opcode OpCode
The instruction opcode.

value sbyte
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, byte value)

opcode OpCode
The instruction opcode.

value byte
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, string value)

opcode OpCode
The instruction opcode.

value string
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, FieldReference field)

opcode OpCode
The instruction opcode.

field FieldReference
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, CallSite site)

opcode OpCode
The instruction opcode.

site CallSite
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, TypeReference type)

opcode OpCode
The instruction opcode.

type TypeReference
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode)

opcode OpCode
The instruction opcode.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, int value)

opcode OpCode
The instruction opcode.

value int
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, MethodReference method)

opcode OpCode
The instruction opcode.

method MethodReference
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, FieldInfo field)

opcode OpCode
The instruction opcode.

field FieldInfo
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, MethodBase method)

opcode OpCode
The instruction opcode.

method MethodBase
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, Type type)

opcode OpCode
The instruction opcode.

type Type
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create(OpCode opcode, object operand)

opcode OpCode
The instruction opcode.

operand object
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

Create a new instruction to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction CreateCall(Delegate method)

method Delegate
The instruction operand.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

If the delegate method isn’t static, its instance must be pushed to the stack first.
The delegate method must not be a lambda expression, as one requires an anonymous instance to be loaded. If it is a lambda expression, use CreateDelegateCall<T>(T) instead.

Creates instructions to invoke a Delegate as if it were a method. Stack behaviour matches the call OpCode.

Normally a Delegate would need to be invoked by pushing the Delegate instance as the first argument to the stack, after which every other argument is pushed. Then, the Delegate is invoked with a callvirt instruction.

With this method, the Delegate instance is automatically stored and loaded for you, and you can simply push all arguments to the stack and then call this method to create the required instructions to invoke the Delegate.

C#
public Instruction[] CreateDelegateCall<T>(T @delegate) where T : Delegate

delegate T
The Delegate method to be invoked.

Instruction[]
An array of Instruction, containing all the instructions required to invoke the Delegate.

Create a new instruction accessing a given member, to be emitted by InsertBeforeCurrent(params IEnumerable<Instruction>) or any of the variations.

C#
public Instruction Create<T>(OpCode opcode, string memberName)

opcode OpCode
The instruction opcode.

memberName string
The accessed member name.

Instruction
The created instruction.

ArgumentNullException

ArgumentException

NotSupportedException

Gets all branching labels pointing to the provided instruction.

C#
public IEnumerable<ILLabel> GetIncomingLabelsFor(Instruction target)

target Instruction
The target instruction for the labels.

IEnumerable<ILLabel>

Gets all branching labels pointing to Current.

C#
public IEnumerable<ILLabel> GetIncomingLabelsForCurrent()

IEnumerable<ILLabel>

RetargetLabels(IEnumerable<ILLabel>, Instruction)

Section titled “RetargetLabels(IEnumerable<ILLabel>, Instruction)”

Retargets ILLabels to a target instruction.

C#
public ILWeaver RetargetLabels(IEnumerable<ILLabel> labels, Instruction target)

labels IEnumerable<ILLabel>
The labels to retarget.

target Instruction
The new target instruction for labels.

ILWeaver
This ILWeaver

Retargets ILLabels targeting source instruction to target instruction.

C#
public ILWeaver RetargetLabels(Instruction source, Instruction target)

source Instruction
The instruction whose labels to retarget.

target Instruction
The new target instruction for labels.

ILWeaver
This ILWeaver

Retargets ILLabels to a target instruction.

C#
public ILWeaver RetargetLabels(ILLabel? label, Instruction target)

label MonoMod.Cil.ILLabel
The label to retarget.

target Instruction
The new target instruction for labels.

ILWeaver
This ILWeaver

Defines a new MonoMod.Cil.ILLabel to be targeted.

C#
public ILLabel DefineLabel()

MonoMod.Cil.ILLabel
The new MonoMod.Cil.ILLabel.

Defines a new MonoMod.Cil.ILLabel to be targeted.

C#
public ILWeaver DefineLabel(out ILLabel label)

label MonoMod.Cil.ILLabel
The new label.

ILWeaver
This ILWeaver.

Sets the target of a label to the provided Instruction.

C#
public ILWeaver MarkLabelTo(Instruction target, ILLabel label)

target Instruction
The target for the label.

label MonoMod.Cil.ILLabel
The label to mark.

ILWeaver
This ILWeaver.

DefineAndMarkLabelTo(Instruction, out ILLabel)

Section titled “DefineAndMarkLabelTo(Instruction, out ILLabel)”

Creates a new label targetting the provided Instruction.

C#
public ILWeaver DefineAndMarkLabelTo(Instruction target, out ILLabel markedLabel)

target Instruction
The target for the label.

markedLabel MonoMod.Cil.ILLabel
The marked label.

ILWeaver
This ILWeaver.

Creates a new label targetting the provided Instruction.

C#
public ILLabel DefineAndMarkLabelTo(Instruction target)

target Instruction
The target for the label.

MonoMod.Cil.ILLabel
The new MonoMod.Cil.ILLabel.

Sets the target of a label to the future next inserted instruction.

C#
public ILWeaver MarkLabelToFutureNextInsert(ILLabel label)

label MonoMod.Cil.ILLabel
The label to mark.

ILWeaver
This ILWeaver.

DefineAndMarkLabelToFutureNextInsert(out ILLabel)

Section titled “DefineAndMarkLabelToFutureNextInsert(out ILLabel)”

Creates a new label targetting the future next inserted instruction.

C#
public ILWeaver DefineAndMarkLabelToFutureNextInsert(out ILLabel futureMarkedLabel)

futureMarkedLabel MonoMod.Cil.ILLabel
The marked label.

ILWeaver
This ILWeaver.

Creates a new label targetting the future next inserted instruction.

C#
public ILLabel DefineAndMarkLabelToFutureNextInsert()

MonoMod.Cil.ILLabel
The new MonoMod.Cil.ILLabel.

MarkLabelToCurrentOrFutureNextInsert(ILLabel)

Section titled “MarkLabelToCurrentOrFutureNextInsert(ILLabel)”

Sets the target of a label to the future next inserted instruction.
Targets Current as a placeholder.

C#
public ILWeaver MarkLabelToCurrentOrFutureNextInsert(ILLabel label)

label MonoMod.Cil.ILLabel
The label to mark.

ILWeaver
This ILWeaver.

Prefer MarkLabelToFutureNextInsert(ILLabel) if the label will always be redirected to an inserted instruction. Using this method will then show that there branches where a next instruction isn’t inserted.

DefineAndMarkLabelToCurrentOrFutureNextInsert(out ILLabel)

Section titled “DefineAndMarkLabelToCurrentOrFutureNextInsert(out ILLabel)”

Creates a new label targetting the future next inserted instruction.
Targets Current as a placeholder.

C#
public ILWeaver DefineAndMarkLabelToCurrentOrFutureNextInsert(out ILLabel futureMarkedLabel)

futureMarkedLabel MonoMod.Cil.ILLabel
The marked label.

ILWeaver
This ILWeaver.

Prefer DefineAndMarkLabelToFutureNextInsert(out ILLabel) if the label will always be redirected to an inserted instruction. Using this method will then show that there branches where a next instruction isn’t inserted.

DefineAndMarkLabelToCurrentOrFutureNextInsert()

Section titled “DefineAndMarkLabelToCurrentOrFutureNextInsert()”

Creates a new label targetting the future next inserted instruction.
Targets Current as a placeholder.

C#
public ILLabel DefineAndMarkLabelToCurrentOrFutureNextInsert()

MonoMod.Cil.ILLabel
The new MonoMod.Cil.ILLabel.

Prefer DefineAndMarkLabelToFutureNextInsert(out ILLabel) if the label will always be redirected to an inserted instruction. Using this method will then show that there branches where a next instruction isn’t inserted.

Sets the target of a label to Current.

C#
public ILWeaver MarkLabelToCurrent(ILLabel label)

label MonoMod.Cil.ILLabel
The label to mark.

ILWeaver
This ILWeaver.

Creates a new label targetting Current.

C#
public ILWeaver DefineAndMarkLabelToCurrent(out ILLabel markedLabel)

markedLabel MonoMod.Cil.ILLabel
The marked label.

ILWeaver
This ILWeaver.

Creates a new label targetting Current.

C#
public ILLabel DefineAndMarkLabelToCurrent()

MonoMod.Cil.ILLabel
The new MonoMod.Cil.ILLabel.

Sets the target of a label to Current’s Previous.

C#
public ILWeaver MarkLabelToCurrentPrevious(ILLabel label)

label MonoMod.Cil.ILLabel
The label to mark.

ILWeaver
This ILWeaver.

The label will point to the Previous instruction at the moment of calling this method.

DefineAndMarkLabelToCurrentPrevious(out ILLabel)

Section titled “DefineAndMarkLabelToCurrentPrevious(out ILLabel)”

Creates a new label targetting Current’s Previous.

C#
public ILWeaver DefineAndMarkLabelToCurrentPrevious(out ILLabel markedLabel)

markedLabel MonoMod.Cil.ILLabel
The marked label.

ILWeaver
This ILWeaver.

The label will point to the Previous instruction at the moment of calling this method.

Creates a new label targetting Current’s Previous.

C#
public ILLabel DefineAndMarkLabelToCurrentPrevious()

MonoMod.Cil.ILLabel
The new MonoMod.Cil.ILLabel.

The label will point to the Previous instruction at the moment of calling this method.

Sets the target of a label to Current’s Next.

C#
public ILWeaver MarkLabelToCurrentNext(ILLabel label)

label MonoMod.Cil.ILLabel
The label to mark.

ILWeaver
This ILWeaver.

The label will point to the Next instruction at the moment of calling this method.

DefineAndMarkLabelToCurrentNext(out ILLabel)

Section titled “DefineAndMarkLabelToCurrentNext(out ILLabel)”

Creates a new label targetting Current’s Next.

C#
public ILWeaver DefineAndMarkLabelToCurrentNext(out ILLabel markedLabel)

markedLabel MonoMod.Cil.ILLabel
The marked label.

ILWeaver
This ILWeaver.

The label will point to the Next instruction at the moment of calling this method.

Creates a new label targetting Current’s Next.

C#
public ILLabel DefineAndMarkLabelToCurrentNext()

MonoMod.Cil.ILLabel
The new MonoMod.Cil.ILLabel.

The label will point to the Next instruction at the moment of calling this method.

Set Current to a target index. See also CurrentTo(Instruction)

C#
public ILWeaver CurrentTo(int index)

index int

ILWeaver
this ILWeaver

IndexOutOfRangeException

A negative index will loop back.

Set Current to a target instruction. See also CurrentTo(int)

For use in MatchRelaxed(params Predicate<Instruction>[]) and other variations, use SetCurrentTo(Instruction) as that method returns true.

C#
[MemberNotNull("current")]
public ILWeaver CurrentTo(Instruction instruction)

instruction Instruction

ILWeaver
this ILWeaver

ArgumentNullException

Set Current to a target instruction like CurrentTo(Instruction), except for use in MatchRelaxed(params Predicate<Instruction>[]) and other variations.

C#
public bool SetCurrentTo(Instruction instruction)

instruction Instruction
The instruction to set as current.

bool
Whether or not the instruction exists in the current method body.

SetInstructionTo(ref Instruction?, Instruction)

Section titled “SetInstructionTo(ref Instruction?, Instruction)”

Set instruction to a target instruction for use in MatchRelaxed(params Predicate<Instruction>[]) and other variations.

C#
public bool SetInstructionTo(ref Instruction? toBeSet, Instruction target)

toBeSet Instruction
The instruction to be set.

target Instruction
The instruction toBeSet will be set to.

bool
Whether or not the target instruction exists in the current method body.

Sets Current to the instruction after it.

C#
public ILWeaver CurrentToNext()

ILWeaver
This ILWeaver.

Sets Current to the instruction before it.

C#
public ILWeaver CurrentToPrevious()

ILWeaver
This ILWeaver.

GetStackSizeZeroBeforeContinuous(Instruction, IInformationalMethodBody?)

Section titled “GetStackSizeZeroBeforeContinuous(Instruction, IInformationalMethodBody?)”

Gets the first reachable instruction backward whose incoming stack size is 0, without any branching.

C#
public Instruction GetStackSizeZeroBeforeContinuous(Instruction start, IInformationalMethodBody? informationalBody = null)

start Instruction
The instruction to start searching at.

informationalBody IInformationalMethodBody
The IInformationalMethodBody whose IInformationalInstructions’ stack size to check. An IInformationalMethodBody should not be reused after the method has been modified.

Instruction

GetStackSizeZeroAfterContinuous(Instruction, IInformationalMethodBody?)

Section titled “GetStackSizeZeroAfterContinuous(Instruction, IInformationalMethodBody?)”

Gets the first reachable instruction forward whose stack size is 0, without any branching.

C#
public Instruction GetStackSizeZeroAfterContinuous(Instruction start, IInformationalMethodBody? informationalBody = null)

start Instruction
The instruction to start searching at.

informationalBody IInformationalMethodBody
The IInformationalMethodBody whose IInformationalInstructions’ stack size to check. An IInformationalMethodBody should not be reused after the method has been modified.

Instruction

NullReferenceException

GetStackSizeZeroAreaContinuous(Instruction, IInformationalMethodBody?)

Section titled “GetStackSizeZeroAreaContinuous(Instruction, IInformationalMethodBody?)”

Gets the both the first reachable instruction backward whose incoming stack size is 0, and the first instruction forward whose stack size is 0, without any branching.

C#
public (Instruction start, Instruction end) GetStackSizeZeroAreaContinuous(Instruction start, IInformationalMethodBody? informationalBody = null)

start Instruction

informationalBody IInformationalMethodBody

(Instruction start, Instruction end)
A tuple with the start and end instructions.

MatchRelaxed(params Predicate<Instruction>[])

Section titled “MatchRelaxed(params Predicate<Instruction>[])”

Attempts to match a set of predicates to find one specific location in the instructions. This method searches the entire target method to ensure the match predicates are matching exactly what was attempted to match.

If the match fails, the match is attempted again against the “original” instructions of the method before it was manipulated. As such, you must NEVER offset Current to another predicate by index, as there may be instructions in between, or that instruction may not even exist in the current method body.

If you want to match multiple locations, use MatchMultipleRelaxed(Action<ILWeaver>, params Predicate<Instruction>[])

In the following example we match two instructions, setting Current to the brtrue instruction which remains as the Current if the match is successful.

C#
weaver
.MatchRelaxed(
x => x.MatchLdloc(1),
x => x.MatchBrtrue(out _) && weaver.SetCurrentTo(x)
)
.ThrowIfFailure()
.InsertBeforeCurrent(weaver.Create(OpCodes.Call, GetCustomNumber));
C#
public ILWeaverResult MatchRelaxed(params Predicate<Instruction>[] predicates)

predicates Predicate<Instruction>[]
The predicates to match against.

ILWeaverResult
An ILWeaverResult which can be used for checking if the match was a success or a failure.

MatchStrict(params Predicate<Instruction>[])

Section titled “MatchStrict(params Predicate<Instruction>[])”

Attempts to match a set of predicates to find one specific location in the instructions. This method searches the entire target method to ensure the match predicates are matching exactly what was attempted to match.

Only a 1:1 match of the current method instructions matching predicates in order are accepted unlike with MatchRelaxed(params Predicate<Instruction>[]). As such you should use that method instead to keep your match more compatible with other mods, unless if you really know what you are doing.

If you want to match multiple locations, use MatchMultipleStrict(Action<ILWeaver>, params Predicate<Instruction>[])

In the following example we match two instructions, setting Current to the brtrue instruction which remains as the Current if the match is successful.

C#
weaver
.MatchStrict(
x => x.MatchLdloc(1),
x => x.MatchBrtrue(out _) && weaver.SetCurrentTo(x)
)
.ThrowIfFailure()
.InsertBeforeCurrent(weaver.Create(OpCodes.Call, GetCustomNumber));
C#
public ILWeaverResult MatchStrict(params Predicate<Instruction>[] predicates)

predicates Predicate<Instruction>[]
The predicates to match against.

ILWeaverResult
An ILWeaverResult which can be used for checking if the match was a success or a failure.

MatchMultipleRelaxed(Action<ILWeaver>, params Predicate<Instruction>[])

Section titled “MatchMultipleRelaxed(Action<ILWeaver>, params Predicate<Instruction>[])”

Attempts to match a set of predicates multiple times to find specific locations in the instructions.

If the match fails, the match is attempted again against the “original” instructions of the method before it was manipulated. As such, you must NEVER offset Current to another predicate by index, as there may be instructions in between, or that instruction may not even exist in the current method body.

In the following example we match two instructions, setting Current to the brtrue instruction which only applies to the ILWeaver clones which are passed to the onMatch delegate’s argument. This means that the ILWeaver you run this method on will keep its original Current even if it’s set in the predicates.

C#
weaver
.MatchMultipleRelaxed(
onMatch: matchWeaver =>
{
matchWeaver.InsertBeforeCurrent(
matchWeaver.Create(OpCodes.Call, GetCustomNumber)
);
},
x => x.MatchLdloc(1),
x => x.MatchBrtrue(out _) && weaver.SetCurrentTo(x)
)
.ThrowIfFailure();
C#
public ILWeaverResult MatchMultipleRelaxed(Action<ILWeaver> onMatch, params Predicate<Instruction>[] predicates)

onMatch Action<ILWeaver>
A delegate which runs for each match, passing a copy of the original ILWeaver with the Current pointing to the one at the time of the match.

predicates Predicate<Instruction>[]
The predicates to match against.

ILWeaverResult
An ILWeaverResult which can be used for checking if the match was a success or a failure.

MatchMultipleStrict(Action<ILWeaver>, params Predicate<Instruction>[])

Section titled “MatchMultipleStrict(Action<ILWeaver>, params Predicate<Instruction>[])”

Attempts to match a set of predicates multiple times to find specific locations in the instructions.

Only a 1:1 match of the current method instructions matching predicates in order are accepted unlike with MatchMultipleRelaxed(Action<ILWeaver>, params Predicate<Instruction>[]). As such you should use that method instead to keep your match more compatible with other mods, unless if you really know what you are doing.

In the following example we match two instructions, setting Current to the brtrue instruction which only applies to the ILWeaver clones which are passed to the onMatch delegate’s argument. This means that the ILWeaver you run this method on will keep its original Current even if it’s set in the predicates.

C#
weaver
.MatchMultipleStrict(
onMatch: matchWeaver =>
{
matchWeaver.InsertBeforeCurrent(
matchWeaver.Create(OpCodes.Call, GetCustomNumber)
);
},
x => x.MatchLdloc(1),
x => x.MatchBrtrue(out _) && weaver.SetCurrentTo(x)
)
.ThrowIfFailure();
C#
public ILWeaverResult MatchMultipleStrict(Action<ILWeaver> onMatch, params Predicate<Instruction>[] predicates)

onMatch Action<ILWeaver>
A delegate which runs for each match, passing a copy of the original ILWeaver with the Current pointing to the one at the time of the match.

predicates Predicate<Instruction>[]
The predicates to match against.

ILWeaverResult
An ILWeaverResult which can be used for checking if the match was a success or a failure.