Class ILCursor
A cursor used to manipulate a method body in an ILContext.
Inheritance
Namespace: MonoMod.Cil
Assembly: MonoMod.Utils.dll
Syntax
public class ILCursor : Object
Constructors
ILCursor(ILContext)
Declaration
public ILCursor(ILContext context)
Parameters
| Type | Name | Description |
|---|---|---|
| ILContext | context |
ILCursor(ILCursor)
Declaration
public ILCursor(ILCursor c)
Parameters
| Type | Name | Description |
|---|---|---|
| ILCursor | c |
Properties
Body
See Body
Declaration
public MethodBody Body { get; }
Property Value
| Type | Description |
|---|---|
| Mono.Cecil.Cil.MethodBody |
Context
The context to which this cursor belongs to.
Declaration
public ILContext Context { get; }
Property Value
| Type | Description |
|---|---|
| ILContext |
IL
See IL
Declaration
public ILProcessor IL { get; }
Property Value
| Type | Description |
|---|---|
| Mono.Cecil.Cil.ILProcessor |
IncomingLabels
Enumerates all labels which point to the current instruction (label.Target == Next)
Declaration
public IEnumerable<ILLabel> IncomingLabels { get; }
Property Value
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<ILLabel> |
Index
The index of the instruction immediately following the cursor position. Range: 0 to Instrs.Count
Setter accepts negative indexing by adding Instrs.Count to the operand
Declaration
public int Index { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Int32 |
Instrs
See Instrs
Declaration
public Collection<Instruction> Instrs { get; }
Property Value
| Type | Description |
|---|---|
| Mono.Collections.Generic.Collection<Mono.Cecil.Cil.Instruction> |
Method
See Method
Declaration
public MethodDefinition Method { get; }
Property Value
| Type | Description |
|---|---|
| Mono.Cecil.MethodDefinition |
Module
See Module
Declaration
public ModuleDefinition Module { get; }
Property Value
| Type | Description |
|---|---|
| Mono.Cecil.ModuleDefinition |
Next
The instruction immediately following the cursor position or null if the cursor is at the end of the instruction list.
Declaration
public Instruction Next { get; set; }
Property Value
| Type | Description |
|---|---|
| Mono.Cecil.Cil.Instruction |
Prev
The instruction immediately preceding the cursor position or null if the cursor is at the start of the instruction list.
Declaration
public Instruction Prev { get; set; }
Property Value
| Type | Description |
|---|---|
| Mono.Cecil.Cil.Instruction |
Previous
The instruction immediately preceding the cursor position or null if the cursor is at the start of the instruction list.
Declaration
public Instruction Previous { get; set; }
Property Value
| Type | Description |
|---|---|
| Mono.Cecil.Cil.Instruction |
SearchTarget
Indicates whether the position of a MMILCursor is the result of a search function and if the next search should ignore the instruction preceeding or following this cursor.
See SearchTarget
Declaration
public SearchTarget SearchTarget { get; set; }
Property Value
| Type | Description |
|---|---|
| SearchTarget |
Methods
AddReference<T>(T)
Bind an arbitary object to an ILContext for static retrieval. See AddReference<T>(T)
Declaration
public int AddReference<T>(T t)
Parameters
| Type | Name | Description |
|---|---|---|
| T | t |
Returns
| Type | Description |
|---|---|
| System.Int32 |
Type Parameters
| Name | Description |
|---|---|
| T |
Clone()
Create a clone of this cursor.
Declaration
public ILCursor Clone()
Returns
| Type | Description |
|---|---|
| ILCursor | The cloned cursor. |
DefineLabel()
Create a new label for use with MarkLabel(ILLabel)
Declaration
public ILLabel DefineLabel()
Returns
| Type | Description |
|---|---|
| ILLabel | A new label with no target |
Emit(OpCode)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, CallSite)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, CallSite site)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| Mono.Cecil.CallSite | site | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, Instruction)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, Instruction target)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| Mono.Cecil.Cil.Instruction | target | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, Instruction[])
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, Instruction[] targets)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| Mono.Cecil.Cil.Instruction[] | targets | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, VariableDefinition)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, VariableDefinition variable)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| Mono.Cecil.Cil.VariableDefinition | variable | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, FieldReference)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, FieldReference field)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| Mono.Cecil.FieldReference | field | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, MethodReference)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, MethodReference method)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| Mono.Cecil.MethodReference | method | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, ParameterDefinition)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, ParameterDefinition parameter)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| Mono.Cecil.ParameterDefinition | parameter | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, TypeReference)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, TypeReference type)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| Mono.Cecil.TypeReference | type | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, Byte)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, byte value)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| System.Byte | value | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, Double)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, double value)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| System.Double | value | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, Int32)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, int value)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| System.Int32 | value | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, Int64)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, long value)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| System.Int64 | value | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, Object)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, object operand)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| System.Object | operand | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, FieldInfo)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, FieldInfo field)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| System.Reflection.FieldInfo | field | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, MethodBase)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, MethodBase method)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| System.Reflection.MethodBase | method | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, SByte)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, sbyte value)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| System.SByte | value | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, Single)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, float value)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| System.Single | value | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, String)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, string value)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| System.String | value | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit(OpCode, Type)
Emit a new instruction at this cursor's current position.
Declaration
public ILCursor Emit(OpCode opcode, Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| System.Type | type | The instruction operand. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Emit<T>(OpCode, String)
Emit a new instruction at this cursor's current position, accessing a given member.
Declaration
public ILCursor Emit<T>(OpCode opcode, string memberName)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.OpCode | opcode | The instruction opcode. |
| System.String | memberName | The accessed member name. |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Type Parameters
| Name | Description |
|---|---|
| T | The type in which the member is defined. |
EmitDelegate<T>(T)
Emit the IL to invoke a delegate as if it were a method. Stack behaviour matches OpCodes.Call
Declaration
public int EmitDelegate<T>(T cb)
where T : Delegate
Parameters
| Type | Name | Description |
|---|---|---|
| T | cb |
Returns
| Type | Description |
|---|---|
| System.Int32 |
Type Parameters
| Name | Description |
|---|---|
| T |
EmitGetReference<T>(Int32)
Emit the IL to retrieve a stored reference of type T with the given id and place it on the stack.
Declaration
public void EmitGetReference<T>(int id)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | id |
Type Parameters
| Name | Description |
|---|---|
| T |
EmitReference<T>(T)
Store an object in the reference store, and emit the IL to retrieve it and place it on the stack.
Declaration
public int EmitReference<T>(T t)
Parameters
| Type | Name | Description |
|---|---|---|
| T | t |
Returns
| Type | Description |
|---|---|
| System.Int32 |
Type Parameters
| Name | Description |
|---|---|
| T |
FindNext(out ILCursor[], Func<Instruction, Boolean>[])
Find the next occurences of a series of instructions matching the given set of predicates with gaps permitted.
Declaration
public void FindNext(out ILCursor[] cursors, params Func<Instruction, bool>[] predicates)
Parameters
| Type | Name | Description |
|---|---|---|
| ILCursor[] | cursors | An array of cursors corresponding to each found instruction (MoveType.Before) |
| System.Func<Mono.Cecil.Cil.Instruction, System.Boolean>[] | predicates |
Exceptions
| Type | Condition |
|---|---|
| System.Collections.Generic.KeyNotFoundException | If no match is found |
FindPrev(out ILCursor[], Func<Instruction, Boolean>[])
Search backwards for occurences of a series of instructions matching the given set of predicates with gaps permitted.
Declaration
public void FindPrev(out ILCursor[] cursors, params Func<Instruction, bool>[] predicates)
Parameters
| Type | Name | Description |
|---|---|---|
| ILCursor[] | cursors | An array of cursors corresponding to each found instruction (MoveType.Before) |
| System.Func<Mono.Cecil.Cil.Instruction, System.Boolean>[] | predicates |
Exceptions
| Type | Condition |
|---|---|
| System.Collections.Generic.KeyNotFoundException | If no match is found |
Goto(Instruction, MoveType, Boolean)
Move the cursor to a target instruction. All other movements go through this.
Declaration
public ILCursor Goto(Instruction insn, MoveType moveType, bool setTarget = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.Instruction | insn | The target instruction |
| MoveType | moveType | Where to move in relation to the target instruction and incoming labels (branches) |
| System.Boolean | setTarget | Whether to set the |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Goto(Int32, MoveType, Boolean)
Move the cursor to a target index. Supports negative indexing. See Goto(Instruction, MoveType, Boolean)
Declaration
public ILCursor Goto(int index, MoveType moveType, bool setTarget = false)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | index | |
| MoveType | moveType | |
| System.Boolean | setTarget |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
GotoLabel(ILLabel, MoveType, Boolean)
Overload for Goto(label.Target). moveType defaults to MoveType.AfterLabel
Declaration
public ILCursor GotoLabel(ILLabel label, MoveType moveType, bool setTarget = false)
Parameters
| Type | Name | Description |
|---|---|---|
| ILLabel | label | |
| MoveType | moveType | |
| System.Boolean | setTarget |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
GotoNext(MoveType, Func<Instruction, Boolean>[])
Search forward and moves the cursor to the next sequence of instructions matching the corresponding predicates. See also TryGotoNext(MoveType, Func<Instruction, Boolean>[])
Declaration
public ILCursor GotoNext(MoveType moveType, params Func<Instruction, bool>[] predicates)
Parameters
| Type | Name | Description |
|---|---|---|
| MoveType | moveType | |
| System.Func<Mono.Cecil.Cil.Instruction, System.Boolean>[] | predicates |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Exceptions
| Type | Condition |
|---|---|
| System.Collections.Generic.KeyNotFoundException | If no match is found |
GotoNext(Func<Instruction, Boolean>[])
Declaration
public ILCursor GotoNext(params Func<Instruction, bool>[] predicates)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Func<Mono.Cecil.Cil.Instruction, System.Boolean>[] | predicates |
Returns
| Type | Description |
|---|---|
| ILCursor |
GotoPrev(MoveType, Func<Instruction, Boolean>[])
Search backward and moves the cursor to the next sequence of instructions matching the corresponding predicates. See also TryGotoPrev(MoveType, Func<Instruction, Boolean>[])
Declaration
public ILCursor GotoPrev(MoveType moveType, params Func<Instruction, bool>[] predicates)
Parameters
| Type | Name | Description |
|---|---|---|
| MoveType | moveType | |
| System.Func<Mono.Cecil.Cil.Instruction, System.Boolean>[] | predicates |
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Exceptions
| Type | Condition |
|---|---|
| System.Collections.Generic.KeyNotFoundException | If no match is found |
GotoPrev(Func<Instruction, Boolean>[])
Declaration
public ILCursor GotoPrev(params Func<Instruction, bool>[] predicates)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Func<Mono.Cecil.Cil.Instruction, System.Boolean>[] | predicates |
Returns
| Type | Description |
|---|---|
| ILCursor |
IsAfter(Instruction)
Is this cursor after the given instruction?
Declaration
public bool IsAfter(Instruction instr)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.Instruction | instr | The instruction to check. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if this cursor is after the given instruction, false otherwise. |
IsBefore(Instruction)
Is this cursor before the given instruction?
Declaration
public bool IsBefore(Instruction instr)
Parameters
| Type | Name | Description |
|---|---|---|
| Mono.Cecil.Cil.Instruction | instr | The instruction to check. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if this cursor is before the given instruction, false otherwise. |
MarkLabel()
Create a new label targetting the current position (label.Target = Next) and moves after it.
Declaration
public ILLabel MarkLabel()
Returns
| Type | Description |
|---|---|
| ILLabel | The newly created label |
MarkLabel(ILLabel)
Set the target of a label to the current position (label.Target = Next) and moves after it.
Declaration
public void MarkLabel(ILLabel label)
Parameters
| Type | Name | Description |
|---|---|---|
| ILLabel | label | The label to mark |
MoveAfterLabels()
Move the cursor after incoming labels (branches). If an instruction is emitted, all labels which currently point to Next, will point to the newly emitted instruction.
Declaration
public ILCursor MoveAfterLabels()
Returns
| Type | Description |
|---|---|
| ILCursor | this |
MoveBeforeLabels()
Move the cursor before incoming labels (branches). This is the default behaviour. Emitted instructions will not cause labels to change targets.
Declaration
public ILCursor MoveBeforeLabels()
Returns
| Type | Description |
|---|---|
| ILCursor | this |
Remove()
Remove the Next instruction
Declaration
public ILCursor Remove()
Returns
| Type | Description |
|---|---|
| ILCursor |
RemoveRange(Int32)
Remove several instructions
Declaration
public ILCursor RemoveRange(int num)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | num |
Returns
| Type | Description |
|---|---|
| ILCursor |
ToString()
Obtain a string representation of this cursor (method ID, index, search target, surrounding instructions).
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| System.String | A string representation of this cursor. |
TryFindNext(out ILCursor[], Func<Instruction, Boolean>[])
Find the next occurences of a series of instructions matching the given set of predicates with gaps permitted.
Declaration
public bool TryFindNext(out ILCursor[] cursors, params Func<Instruction, bool>[] predicates)
Parameters
| Type | Name | Description |
|---|---|---|
| ILCursor[] | cursors | An array of cursors corresponding to each found instruction (MoveType.Before) |
| System.Func<Mono.Cecil.Cil.Instruction, System.Boolean>[] | predicates |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if a match was found |
TryFindPrev(out ILCursor[], Func<Instruction, Boolean>[])
Search backwards for occurences of a series of instructions matching the given set of predicates with gaps permitted.
Declaration
public bool TryFindPrev(out ILCursor[] cursors, params Func<Instruction, bool>[] predicates)
Parameters
| Type | Name | Description |
|---|---|---|
| ILCursor[] | cursors | An array of cursors corresponding to each found instruction (MoveType.Before) |
| System.Func<Mono.Cecil.Cil.Instruction, System.Boolean>[] | predicates |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if a match was found |
TryGotoNext(MoveType, Func<Instruction, Boolean>[])
Search forward and moves the cursor to the next sequence of instructions matching the corresponding predicates.
Declaration
public bool TryGotoNext(MoveType moveType, params Func<Instruction, bool>[] predicates)
Parameters
| Type | Name | Description |
|---|---|---|
| MoveType | moveType | |
| System.Func<Mono.Cecil.Cil.Instruction, System.Boolean>[] | predicates |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if a match was found |
TryGotoNext(Func<Instruction, Boolean>[])
Declaration
public bool TryGotoNext(params Func<Instruction, bool>[] predicates)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Func<Mono.Cecil.Cil.Instruction, System.Boolean>[] | predicates |
Returns
| Type | Description |
|---|---|
| System.Boolean |
TryGotoPrev(MoveType, Func<Instruction, Boolean>[])
Search backward and moves the cursor to the next sequence of instructions matching the corresponding predicates.
Declaration
public bool TryGotoPrev(MoveType moveType, params Func<Instruction, bool>[] predicates)
Parameters
| Type | Name | Description |
|---|---|---|
| MoveType | moveType | |
| System.Func<Mono.Cecil.Cil.Instruction, System.Boolean>[] | predicates |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if a match was found |
TryGotoPrev(Func<Instruction, Boolean>[])
Declaration
public bool TryGotoPrev(params Func<Instruction, bool>[] predicates)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Func<Mono.Cecil.Cil.Instruction, System.Boolean>[] | predicates |
Returns
| Type | Description |
|---|---|
| System.Boolean |