Show / Hide Table of Contents

Class CodeInstruction

An abstract wrapper around OpCode and their operands. Used by transpilers

Inheritance
object
CodeInstruction
CodeMatch
Namespace: HarmonyLib
Assembly: 0Harmony.dll
Syntax
public class CodeInstruction

Constructors

CodeInstruction(CodeInstruction)

Create a full copy (including labels and exception blocks) of a CodeInstruction

Declaration
public CodeInstruction(CodeInstruction instruction)
Parameters
Type Name Description
CodeInstruction instruction

The CodeInstruction to copy

CodeInstruction(OpCode, object)

Creates a new CodeInstruction with a given opcode and optional operand

Declaration
public CodeInstruction(OpCode opcode, object operand = null)
Parameters
Type Name Description
OpCode opcode

The opcode

object operand

The operand

Fields

blocks

All exception block boundaries defined on this instruction

Declaration
public List<ExceptionBlock> blocks
Field Value
Type Description
List<ExceptionBlock>

labels

All labels defined on this instruction

Declaration
public List<Label> labels
Field Value
Type Description
List<Label>

opcode

The opcode

Declaration
public OpCode opcode
Field Value
Type Description
OpCode

operand

The operand

Declaration
public object operand
Field Value
Type Description
object

Methods

Call(Expression<Action>)

Creates a CodeInstruction calling a method (CALL)

Declaration
public static CodeInstruction Call(Expression<Action> expression)
Parameters
Type Name Description
Expression<Action> expression

The lambda expression using the method

Returns
Type Description
CodeInstruction

A new Codeinstruction

Call(LambdaExpression)

Creates a CodeInstruction calling a method (CALL)

Declaration
public static CodeInstruction Call(LambdaExpression expression)
Parameters
Type Name Description
LambdaExpression expression

The lambda expression using the method

Returns
Type Description
CodeInstruction

A new Codeinstruction

Call(string, Type[], Type[])

Creates a CodeInstruction calling a method (CALL)

Declaration
public static CodeInstruction Call(string typeColonMethodname, Type[] parameters = null, Type[] generics = null)
Parameters
Type Name Description
string typeColonMethodname

The target method in the form TypeFullName:MethodName, where the type name matches a form recognized by Type.GetType like Some.Namespace.Type.

Type[] parameters

Optional parameters to target a specific overload of the method

Type[] generics

Optional list of types that define the generic version of the method

Returns
Type Description
CodeInstruction

A code instruction that calls the method matching the arguments

Call(Type, string, Type[], Type[])

Creates a CodeInstruction calling a method (CALL)

Declaration
public static CodeInstruction Call(Type type, string name, Type[] parameters = null, Type[] generics = null)
Parameters
Type Name Description
Type type

The class/type where the method is declared

string name

The name of the method (case sensitive)

Type[] parameters

Optional parameters to target a specific overload of the method

Type[] generics

Optional list of types that define the generic version of the method

Returns
Type Description
CodeInstruction

A code instruction that calls the method matching the arguments

CallClosure<T>(T)

Returns an instruction to call the specified closure

Declaration
public static CodeInstruction CallClosure<T>(T closure) where T : Delegate
Parameters
Type Name Description
T closure

The closure that defines the method to call

Returns
Type Description
CodeInstruction

A CodeInstruction that calls the closure as a method

Type Parameters
Name Description
T

The delegate type to emit

Call<T>(Expression<Action<T>>)

Creates a CodeInstruction calling a method (CALL)

Declaration
public static CodeInstruction Call<T>(Expression<Action<T>> expression)
Parameters
Type Name Description
Expression<Action<T>> expression

The lambda expression using the method

Returns
Type Description
CodeInstruction

A new Codeinstruction

Type Parameters
Name Description
T

Call<T, TResult>(Expression<Func<T, TResult>>)

Creates a CodeInstruction calling a method (CALL)

Declaration
public static CodeInstruction Call<T, TResult>(Expression<Func<T, TResult>> expression)
Parameters
Type Name Description
Expression<Func<T, TResult>> expression

The lambda expression using the method

Returns
Type Description
CodeInstruction

A new Codeinstruction

Type Parameters
Name Description
T
TResult

Clone()

Clones a CodeInstruction and resets its labels and exception blocks

Declaration
public CodeInstruction Clone()
Returns
Type Description
CodeInstruction

A lightweight copy of this code instruction

Clone(object)

Clones a CodeInstruction, resets labels and exception blocks and sets its operand

Declaration
public CodeInstruction Clone(object operand)
Parameters
Type Name Description
object operand

The operand

Returns
Type Description
CodeInstruction

A copy of this CodeInstruction with a new operand

Clone(OpCode)

Clones a CodeInstruction, resets labels and exception blocks and sets its opcode

Declaration
public CodeInstruction Clone(OpCode opcode)
Parameters
Type Name Description
OpCode opcode

The opcode

Returns
Type Description
CodeInstruction

A copy of this CodeInstruction with a new opcode

HasBlock(ExceptionBlockType)

Checks if a CodeInstruction contains a given exception block type

Declaration
public bool HasBlock(ExceptionBlockType type)
Parameters
Type Name Description
ExceptionBlockType type

Type of the exception block to check for

Returns
Type Description
bool

True if the instruction contains the exception block type, false otherwise

LoadArgument(int, bool)

Creates a CodeInstruction loading an argument with the given index, using the shorter forms when possible

Declaration
public static CodeInstruction LoadArgument(int index, bool useAddress = false)
Parameters
Type Name Description
int index

The index of the argument

bool useAddress

Use address of argument

Returns
Type Description
CodeInstruction

A new Codeinstruction

See Also
ArgumentIndex(CodeInstruction)

LoadField(Type, string, bool)

Creates a CodeInstruction loading a field (LD[S]FLD[A])

Declaration
public static CodeInstruction LoadField(Type type, string name, bool useAddress = false)
Parameters
Type Name Description
Type type

The class/type where the field is defined

string name

The name of the field (case sensitive)

bool useAddress

Use address of field

Returns
Type Description
CodeInstruction

A new Codeinstruction

LoadLocal(int, bool)

Creates a CodeInstruction loading a local with the given index, using the shorter forms when possible

Declaration
public static CodeInstruction LoadLocal(int index, bool useAddress = false)
Parameters
Type Name Description
int index

The index where the local is stored

bool useAddress

Use address of local

Returns
Type Description
CodeInstruction

A new Codeinstruction

See Also
LocalIndex(CodeInstruction)

StoreArgument(int)

Creates a CodeInstruction storing to an argument with the given index, using the shorter forms when possible

Declaration
public static CodeInstruction StoreArgument(int index)
Parameters
Type Name Description
int index

The index of the argument

Returns
Type Description
CodeInstruction

A new Codeinstruction

See Also
ArgumentIndex(CodeInstruction)

StoreField(Type, string)

Creates a CodeInstruction storing to a field (ST[S]FLD)

Declaration
public static CodeInstruction StoreField(Type type, string name)
Parameters
Type Name Description
Type type

The class/type where the field is defined

string name

The name of the field (case sensitive)

Returns
Type Description
CodeInstruction

A new Codeinstruction

StoreLocal(int)

Creates a CodeInstruction storing to a local with the given index, using the shorter forms when possible

Declaration
public static CodeInstruction StoreLocal(int index)
Parameters
Type Name Description
int index

The index where the local is stored

Returns
Type Description
CodeInstruction

A new Codeinstruction

See Also
LocalIndex(CodeInstruction)

ToString()

Returns a string representation of the code instruction

Declaration
public override string ToString()
Returns
Type Description
string

A string representation of the code instruction

Overrides
object.ToString()

Extension Methods

CodeInstructionExtensions.ArgumentIndex(CodeInstruction)
CodeInstructionExtensions.Branches(CodeInstruction, out Label?)
CodeInstructionExtensions.Calls(CodeInstruction, MethodInfo)
CodeInstructionExtensions.ExtractBlocks(CodeInstruction)
CodeInstructionExtensions.ExtractLabels(CodeInstruction)
CodeInstructionExtensions.Is(CodeInstruction, OpCode, object)
CodeInstructionExtensions.IsLdarg(CodeInstruction, int?)
CodeInstructionExtensions.IsLdarga(CodeInstruction, int?)
CodeInstructionExtensions.IsLdloc(CodeInstruction, LocalBuilder)
CodeInstructionExtensions.IsStarg(CodeInstruction, int?)
CodeInstructionExtensions.IsStloc(CodeInstruction, LocalBuilder)
CodeInstructionExtensions.LoadsConstant(CodeInstruction)
CodeInstructionExtensions.LoadsConstant(CodeInstruction, double)
CodeInstructionExtensions.LoadsConstant(CodeInstruction, Enum)
CodeInstructionExtensions.LoadsConstant(CodeInstruction, long)
CodeInstructionExtensions.LoadsConstant(CodeInstruction, string)
CodeInstructionExtensions.LoadsField(CodeInstruction, FieldInfo, bool)
CodeInstructionExtensions.LocalIndex(CodeInstruction)
CodeInstructionExtensions.MoveBlocksFrom(CodeInstruction, CodeInstruction)
CodeInstructionExtensions.MoveBlocksTo(CodeInstruction, CodeInstruction)
CodeInstructionExtensions.MoveLabelsFrom(CodeInstruction, CodeInstruction)
CodeInstructionExtensions.MoveLabelsTo(CodeInstruction, CodeInstruction)
CodeInstructionExtensions.OperandIs(CodeInstruction, object)
CodeInstructionExtensions.StoresField(CodeInstruction, FieldInfo)
CodeInstructionExtensions.WithBlocks(CodeInstruction, params ExceptionBlock[])
CodeInstructionExtensions.WithBlocks(CodeInstruction, IEnumerable<ExceptionBlock>)
CodeInstructionExtensions.WithLabels(CodeInstruction, IEnumerable<Label>)
CodeInstructionExtensions.WithLabels(CodeInstruction, params Label[])
In this article
Back to top Generated by DocFX