Browse documentation

Class CodeInstructionExtensions

Namespace
HarmonyLib
Assembly
0Harmony.dll

Extensions for CodeInstruction

public static class CodeInstructionExtensions
Inheritance
object
CodeInstructionExtensions

Methods

ArgumentIndex(CodeInstruction)

Returns the index targeted by this ldarg, ldarga, or starg

public static int ArgumentIndex(this CodeInstruction code)

Parameters

code CodeInstruction

The CodeInstruction

Returns

int

The index it targets

See Also
LoadArgument(int, bool)

Branches(CodeInstruction, out Label?)

Tests if the code instruction branches

public static bool Branches(this CodeInstruction code, out Label? label)

Parameters

code CodeInstruction

The CodeInstruction

label Label?

The label if the instruction is a branch operation or null if not

Returns

bool

True if the instruction branches

Calls(CodeInstruction, MethodInfo)

Tests if the code instruction calls the method

public static bool Calls(this CodeInstruction code, MethodInfo method)

Parameters

code CodeInstruction

The CodeInstruction

method MethodInfo

The method

Returns

bool

True if the instruction calls the method

ExtractBlocks(CodeInstruction)

Extracts all ExceptionBlocks from the code instruction and returns them

public static List<ExceptionBlock> ExtractBlocks(this CodeInstruction code)

Parameters

code CodeInstruction

The CodeInstruction

Returns

List<ExceptionBlock>

A list of ExceptionBlock

ExtractLabels(CodeInstruction)

Extracts all labels from the code instruction and returns them

public static List<Label> ExtractLabels(this CodeInstruction code)

Parameters

code CodeInstruction

The CodeInstruction

Returns

List<Label>

A list of System.Reflection.Emit.Label

Is(CodeInstruction, OpCode, object)

Shortcut for

code.opcode == opcode && code.OperandIs(operand)
public static bool Is(this CodeInstruction code, OpCode opcode, object operand)

Parameters

code CodeInstruction

The CodeInstruction

opcode OpCode

The System.Reflection.Emit.OpCode

operand object

The operand value

Returns

bool

True if the opcode is equal to the given opcode and the operand has the same type and is equal to the given operand

IsLdarg(CodeInstruction, int?)

Tests for any form of Ldarg*

public static bool IsLdarg(this CodeInstruction code, int? n = null)

Parameters

code CodeInstruction

The CodeInstruction

n int?

The (optional) index

Returns

bool

True if it matches one of the variations

IsLdarga(CodeInstruction, int?)

Tests for Ldarga/Ldarga_S

public static bool IsLdarga(this CodeInstruction code, int? n = null)

Parameters

code CodeInstruction

The CodeInstruction

n int?

The (optional) index

Returns

bool

True if it matches one of the variations

IsLdloc(CodeInstruction, LocalBuilder)

Tests for any form of Ldloc*

public static bool IsLdloc(this CodeInstruction code, LocalBuilder variable = null)

Parameters

code CodeInstruction

The CodeInstruction

variable LocalBuilder

The optional local variable

Returns

bool

True if it matches one of the variations

IsStarg(CodeInstruction, int?)

Tests for Starg/Starg_S

public static bool IsStarg(this CodeInstruction code, int? n = null)

Parameters

code CodeInstruction

The CodeInstruction

n int?

The (optional) index

Returns

bool

True if it matches one of the variations

IsStloc(CodeInstruction, LocalBuilder)

Tests for any form of Stloc*

public static bool IsStloc(this CodeInstruction code, LocalBuilder variable = null)

Parameters

code CodeInstruction

The CodeInstruction

variable LocalBuilder

The optional local variable

Returns

bool

True if it matches one of the variations

IsValid(OpCode)

Returns if an System.Reflection.Emit.OpCode is initialized and valid

public static bool IsValid(this OpCode code)

Parameters

code OpCode

The System.Reflection.Emit.OpCode

Returns

bool

LoadsConstant(CodeInstruction)

Tests if the code instruction loads a constant

public static bool LoadsConstant(this CodeInstruction code)

Parameters

code CodeInstruction

The CodeInstruction

Returns

bool

True if the instruction loads a constant

LoadsConstant(CodeInstruction, double)

Tests if the code instruction loads a floating point constant

public static bool LoadsConstant(this CodeInstruction code, double number)

Parameters

code CodeInstruction

The CodeInstruction

number double

The floating point constant

Returns

bool

True if the instruction loads the constant

LoadsConstant(CodeInstruction, Enum)

Tests if the code instruction loads an enum constant

public static bool LoadsConstant(this CodeInstruction code, Enum e)

Parameters

code CodeInstruction

The CodeInstruction

e Enum

The enum

Returns

bool

True if the instruction loads the constant

LoadsConstant(CodeInstruction, long)

Tests if the code instruction loads an integer constant

public static bool LoadsConstant(this CodeInstruction code, long number)

Parameters

code CodeInstruction

The CodeInstruction

number long

The integer constant

Returns

bool

True if the instruction loads the constant

LoadsConstant(CodeInstruction, string)

Tests if the code instruction loads a string constant

public static bool LoadsConstant(this CodeInstruction code, string str)

Parameters

code CodeInstruction

The CodeInstruction

str string

The string

Returns

bool

True if the instruction loads the constant

LoadsField(CodeInstruction, FieldInfo, bool)

Tests if the code instruction loads a field

public static bool LoadsField(this CodeInstruction code, FieldInfo field, bool byAddress = false)

Parameters

code CodeInstruction

The CodeInstruction

field FieldInfo

The field

byAddress bool

Set to true if the address of the field is loaded

Returns

bool

True if the instruction loads the field

LocalIndex(CodeInstruction)

Returns the index targeted by this ldloc, ldloca, or stloc

public static int LocalIndex(this CodeInstruction code)

Parameters

code CodeInstruction

The CodeInstruction

Returns

int

The index it targets

See Also
LoadLocal(int, bool)

MoveBlocksFrom(CodeInstruction, CodeInstruction)

Moves all ExceptionBlocks from another code instruction to the current one

public static CodeInstruction MoveBlocksFrom(this CodeInstruction code, CodeInstruction other)

Parameters

code CodeInstruction

The CodeInstruction to move the ExceptionBlocks to

other CodeInstruction

The other CodeInstruction to move the ExceptionBlocks from

Returns

CodeInstruction

The code instruction that received the blocks

MoveBlocksTo(CodeInstruction, CodeInstruction)

Moves all ExceptionBlocks from the code instruction to another one

public static CodeInstruction MoveBlocksTo(this CodeInstruction code, CodeInstruction other)

Parameters

code CodeInstruction

The CodeInstruction to move the ExceptionBlocks from

other CodeInstruction

The other CodeInstruction to move the ExceptionBlocks to

Returns

CodeInstruction

The code instruction blocks were moved from (now empty)

MoveLabelsFrom(CodeInstruction, CodeInstruction)

Moves all labels from another code instruction to the current one

public static CodeInstruction MoveLabelsFrom(this CodeInstruction code, CodeInstruction other)

Parameters

code CodeInstruction

The CodeInstruction to move the labels to

other CodeInstruction

The other CodeInstruction to move the labels from

Returns

CodeInstruction

The code instruction that received the labels

MoveLabelsTo(CodeInstruction, CodeInstruction)

Moves all labels from the code instruction to another one

public static CodeInstruction MoveLabelsTo(this CodeInstruction code, CodeInstruction other)

Parameters

code CodeInstruction

The CodeInstruction to move the labels from

other CodeInstruction

The other CodeInstruction to move the labels to

Returns

CodeInstruction

The code instruction labels were moved from (now empty)

OperandIs(CodeInstruction, object)

Shortcut for testing whether the operand is equal to a non-null value

public static bool OperandIs(this CodeInstruction code, object value)

Parameters

code CodeInstruction

The CodeInstruction

value object

The value

Returns

bool

True if the operand has the same type and is equal to the value

StoresField(CodeInstruction, FieldInfo)

Tests if the code instruction stores a field

public static bool StoresField(this CodeInstruction code, FieldInfo field)

Parameters

code CodeInstruction

The CodeInstruction

field FieldInfo

The field

Returns

bool

True if the instruction stores this field

WithBlocks(CodeInstruction, params ExceptionBlock[])

Adds ExceptionBlocks to the code instruction and return it

public static CodeInstruction WithBlocks(this CodeInstruction code, params ExceptionBlock[] blocks)

Parameters

code CodeInstruction

The CodeInstruction

blocks ExceptionBlock[]

One or several ExceptionBlock to add

Returns

CodeInstruction

The same code instruction

WithBlocks(CodeInstruction, IEnumerable<ExceptionBlock>)

Adds ExceptionBlocks to the code instruction and return it

public static CodeInstruction WithBlocks(this CodeInstruction code, IEnumerable<ExceptionBlock> blocks)

Parameters

code CodeInstruction

The CodeInstruction

blocks IEnumerable<ExceptionBlock>

An enumeration of ExceptionBlock

Returns

CodeInstruction

The same code instruction

WithLabels(CodeInstruction, IEnumerable<Label>)

Adds labels to the code instruction and return it

public static CodeInstruction WithLabels(this CodeInstruction code, IEnumerable<Label> labels)

Parameters

code CodeInstruction

The CodeInstruction

labels IEnumerable<Label>

An enumeration of System.Reflection.Emit.Label

Returns

CodeInstruction

The same code instruction

WithLabels(CodeInstruction, params Label[])

Adds labels to the code instruction and return it

public static CodeInstruction WithLabels(this CodeInstruction code, params Label[] labels)

Parameters

code CodeInstruction

The CodeInstruction

labels Label[]

One or several System.Reflection.Emit.Label to add

Returns

CodeInstruction

The same code instruction

Harmony 3 preview

For the stable release, read the 2.x documentation.