Class AssemblyLocator

Namespace
AsmLocator
Assembly
AsmLocator.dll

Lookup naming conventions:

  • FindX returns the first match or null.
  • FindXs lazily enumerates all matches.
  • GetX requires exactly one match and throws MemberNotFoundException otherwise.
  • EditX resolves one match and invokes an edit callback.

FindXs searches every type that matches the TypeSpec, whereas GetX, EditX and the CIL helpers first require the TypeSpec to resolve to exactly one type. A broad TypeSpec therefore works with FindX but makes GetX throw an ambiguity error even when only one of the matched types contains the requested member.

public sealed class AssemblyLocator
Inheritance
AssemblyLocator
Inherited Members

Constructors

AssemblyLocator(ModuleDefinition)

Creates a locator around an existing AsmResolver module.

public AssemblyLocator(ModuleDefinition module)

Parameters

module ModuleDefinition

The module to inspect and edit.

Exceptions

ArgumentNullException

module is null.

Properties

Module

The underlying AsmResolver module being searched or edited.

public ModuleDefinition Module { get; }

Property Value

ModuleDefinition

Methods

EditBody(TypeSpec, EventSpec, EventAccessor, Action<CilMethodBody>)

Invokes the callback to edit a method or accessor body and recalculates instruction offsets afterward.

public void EditBody(TypeSpec type, EventSpec ev, EventAccessor accessor, Action<CilMethodBody> edit)

Parameters

type TypeSpec

The type specification used to select candidate types.

ev EventSpec

The event specification used to select candidate events.

accessor EventAccessor

The property or event accessor to retrieve.

edit Action<CilMethodBody>

The callback that performs the requested edit.

EditBody(TypeSpec, MethodSpec, Action<CilMethodBody>)

Invokes the callback to edit a method body and recalculates instruction offsets afterward.

public void EditBody(TypeSpec type, MethodSpec method, Action<CilMethodBody> edit)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

edit Action<CilMethodBody>

The callback that performs the requested edit.

EditBody(TypeSpec, PropertySpec, PropertyAccessor, Action<CilMethodBody>)

Invokes the callback to edit a method or accessor body and recalculates instruction offsets afterward.

public void EditBody(TypeSpec type, PropertySpec property, PropertyAccessor accessor, Action<CilMethodBody> edit)

Parameters

type TypeSpec

The type specification used to select candidate types.

property PropertySpec

The property specification used to select candidate properties.

accessor PropertyAccessor

The property or event accessor to retrieve.

edit Action<CilMethodBody>

The callback that performs the requested edit.

EditEvent(TypeSpec, EventSpec, Action<EventDefinition>)

Finds exactly one event and invokes the supplied callback to edit it.

public void EditEvent(TypeSpec type, EventSpec ev, Action<EventDefinition> edit)

Parameters

type TypeSpec

The type specification used to select candidate types.

ev EventSpec

The event specification used to select candidate events.

edit Action<EventDefinition>

The callback that performs the requested edit.

EditField(TypeSpec, FieldSpec, Action<FieldDefinition>)

Finds exactly one field and invokes the supplied callback to edit it.

public void EditField(TypeSpec type, FieldSpec field, Action<FieldDefinition> edit)

Parameters

type TypeSpec

The type specification used to select candidate types.

field FieldSpec

The field specification used to select candidate fields.

edit Action<FieldDefinition>

The callback that performs the requested edit.

EditInstructions(TypeSpec, MethodSpec, InstructionSpec, Action<CilMethodBody, InstructionMatch>)

Invokes the callback for each matching instruction, from the last match to the first, to reduce index invalidation when callbacks insert or remove instructions. Throws when no instruction matches.

public void EditInstructions(TypeSpec type, MethodSpec method, InstructionSpec instruction, Action<CilMethodBody, InstructionMatch> edit)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

instruction InstructionSpec

The instruction specification used to select candidate instructions.

edit Action<CilMethodBody, InstructionMatch>

The callback that performs the requested edit.

Exceptions

MemberNotFoundException

The method/body is missing or no instruction matches.

EditMethod(TypeSpec, MethodSpec, Action<MethodDefinition>)

Finds exactly one method and invokes the supplied callback to edit it.

public void EditMethod(TypeSpec type, MethodSpec method, Action<MethodDefinition> edit)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

edit Action<MethodDefinition>

The callback that performs the requested edit.

EditProperty(TypeSpec, PropertySpec, Action<PropertyDefinition>)

Finds exactly one property and invokes the supplied callback to edit it.

public void EditProperty(TypeSpec type, PropertySpec property, Action<PropertyDefinition> edit)

Parameters

type TypeSpec

The type specification used to select candidate types.

property PropertySpec

The property specification used to select candidate properties.

edit Action<PropertyDefinition>

The callback that performs the requested edit.

EditType(TypeSpec, Action<TypeDefinition>)

Finds exactly one type and invokes the supplied callback to edit it.

public void EditType(TypeSpec type, Action<TypeDefinition> edit)

Parameters

type TypeSpec

The type specification used to select candidate types.

edit Action<TypeDefinition>

The callback that performs the requested edit.

FindBody(TypeSpec, MethodSpec)

Returns the body of the first matching method, or null when the method or its body is absent.

public CilMethodBody? FindBody(TypeSpec type, MethodSpec method)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

Returns

CilMethodBody

The first match, or null when nothing matches.

FindEvent(TypeSpec, EventSpec)

Returns the first matching event, or null when no event matches.

public EventDefinition? FindEvent(TypeSpec type, EventSpec ev)

Parameters

type TypeSpec

The type specification used to select candidate types.

ev EventSpec

The event specification used to select candidate events.

Returns

EventDefinition

The first match, or null when nothing matches.

FindEvents(TypeSpec, EventSpec)

Enumerates matching events from every type that matches the supplied type specification.

public IEnumerable<EventDefinition> FindEvents(TypeSpec type, EventSpec ev)

Parameters

type TypeSpec

The type specification used to select candidate types.

ev EventSpec

The event specification used to select candidate events.

Returns

IEnumerable<EventDefinition>

A lazily evaluated sequence of the matching items; empty when nothing matches.

FindField(TypeSpec, FieldSpec)

Returns the first matching field, or null when no field matches.

public FieldDefinition? FindField(TypeSpec type, FieldSpec field)

Parameters

type TypeSpec

The type specification used to select candidate types.

field FieldSpec

The field specification used to select candidate fields.

Returns

FieldDefinition

The first match, or null when nothing matches.

FindFields(TypeSpec, FieldSpec)

Enumerates matching fields from every type that matches the supplied type specification.

public IEnumerable<FieldDefinition> FindFields(TypeSpec type, FieldSpec field)

Parameters

type TypeSpec

The type specification used to select candidate types.

field FieldSpec

The field specification used to select candidate fields.

Returns

IEnumerable<FieldDefinition>

A lazily evaluated sequence of the matching items; empty when nothing matches.

FindInstructions(TypeSpec, MethodSpec, InstructionSpec)

Returns all matching instructions from the uniquely matched method body, including their indexes.

public IReadOnlyList<InstructionMatch> FindInstructions(TypeSpec type, MethodSpec method, InstructionSpec instruction)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

instruction InstructionSpec

The instruction specification used to select candidate instructions.

Returns

IReadOnlyList<InstructionMatch>

All matches in order of occurrence; empty when nothing matches.

FindLocals(TypeSpec, MethodSpec, LocalSpec)

Enumerates local variables from matching method bodies that satisfy the local specification.

public IEnumerable<CilLocalVariable> FindLocals(TypeSpec type, MethodSpec method, LocalSpec local)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

local LocalSpec

The local-variable specification used to select candidate locals.

Returns

IEnumerable<CilLocalVariable>

A lazily evaluated sequence of the matching items; empty when nothing matches.

FindMethod(TypeSpec, MethodSpec)

Returns the first matching method, or null when no method matches.

public MethodDefinition? FindMethod(TypeSpec type, MethodSpec method)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

Returns

MethodDefinition

The first match, or null when nothing matches.

FindMethods(TypeSpec, MethodSpec)

Enumerates matching methods from every type that matches the supplied type specification.

public IEnumerable<MethodDefinition> FindMethods(TypeSpec type, MethodSpec method)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

Returns

IEnumerable<MethodDefinition>

A lazily evaluated sequence of the matching items; empty when nothing matches.

FindParameter(TypeSpec, MethodSpec, ParameterSpec)

Returns the first matching parameter, or null when no parameter matches.

public Parameter? FindParameter(TypeSpec type, MethodSpec method, ParameterSpec parameter)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

parameter ParameterSpec

The parameter specification used to select candidate parameters.

Returns

Parameter

The first match, or null when nothing matches.

FindParameters(TypeSpec, MethodSpec, ParameterSpec)

Enumerates parameters from matching methods that satisfy the parameter specification.

public IEnumerable<Parameter> FindParameters(TypeSpec type, MethodSpec method, ParameterSpec parameter)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

parameter ParameterSpec

The parameter specification used to select candidate parameters.

Returns

IEnumerable<Parameter>

A lazily evaluated sequence of the matching items; empty when nothing matches.

FindPattern(TypeSpec, MethodSpec, params InstructionSpec[])

Returns all contiguous instruction sequences matching the ordered pattern.

public IReadOnlyList<InstructionSequence> FindPattern(TypeSpec type, MethodSpec method, params InstructionSpec[] pattern)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

pattern InstructionSpec[]

The ordered instruction specifications that form the pattern.

Returns

IReadOnlyList<InstructionSequence>

All matches in order of occurrence; empty when nothing matches.

Exceptions

ArgumentException

The pattern is empty or contains a null specification.

FindProperties(TypeSpec, PropertySpec)

Enumerates matching properties from every type that matches the supplied type specification.

public IEnumerable<PropertyDefinition> FindProperties(TypeSpec type, PropertySpec property)

Parameters

type TypeSpec

The type specification used to select candidate types.

property PropertySpec

The property specification used to select candidate properties.

Returns

IEnumerable<PropertyDefinition>

A lazily evaluated sequence of the matching items; empty when nothing matches.

FindProperty(TypeSpec, PropertySpec)

Returns the first matching property, or null when no property matches.

public PropertyDefinition? FindProperty(TypeSpec type, PropertySpec property)

Parameters

type TypeSpec

The type specification used to select candidate types.

property PropertySpec

The property specification used to select candidate properties.

Returns

PropertyDefinition

The first match, or null when nothing matches.

FindType(TypeSpec)

Returns the first type matching the supplied specification, or null when no type matches.

public TypeDefinition? FindType(TypeSpec type)

Parameters

type TypeSpec

The type specification used to select candidate types.

Returns

TypeDefinition

The first match, or null when nothing matches.

FindTypes(TypeSpec)

Enumerates all types matching the supplied specification.

public IEnumerable<TypeDefinition> FindTypes(TypeSpec type)

Parameters

type TypeSpec

The type specification used to select candidate types.

Returns

IEnumerable<TypeDefinition>

A lazily evaluated sequence of the matching items; empty when nothing matches.

FromBytes(byte[])

Loads a .NET module from a byte array and creates a locator for it.

public static AssemblyLocator FromBytes(byte[] data)

Parameters

data byte[]

The byte array containing the assembly image.

Returns

AssemblyLocator

A locator that wraps the loaded module.

FromFile(string)

Loads a .NET module from the specified file and creates a locator for it.

public static AssemblyLocator FromFile(string path)

Parameters

path string

The path of the input assembly or module file.

Returns

AssemblyLocator

A locator that wraps the loaded module.

GetBody(TypeSpec, EventSpec, EventAccessor)

Returns the CIL body of the uniquely matched method or accessor; throws if it is unavailable.

public CilMethodBody GetBody(TypeSpec type, EventSpec ev, EventAccessor accessor)

Parameters

type TypeSpec

The type specification used to select candidate types.

ev EventSpec

The event specification used to select candidate events.

accessor EventAccessor

The property or event accessor to retrieve.

Returns

CilMethodBody

The single matching item.

Exceptions

MemberNotFoundException

The method, accessor, or CIL body is missing, or the method match is ambiguous.

GetBody(TypeSpec, MethodSpec)

Returns the CIL body of the uniquely matched method or accessor; throws if it is unavailable.

public CilMethodBody GetBody(TypeSpec type, MethodSpec method)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

Returns

CilMethodBody

The single matching item.

Exceptions

MemberNotFoundException

The method, accessor, or CIL body is missing, or the method match is ambiguous.

GetBody(TypeSpec, PropertySpec, PropertyAccessor)

Returns the CIL body of the uniquely matched method or accessor; throws if it is unavailable.

public CilMethodBody GetBody(TypeSpec type, PropertySpec property, PropertyAccessor accessor)

Parameters

type TypeSpec

The type specification used to select candidate types.

property PropertySpec

The property specification used to select candidate properties.

accessor PropertyAccessor

The property or event accessor to retrieve.

Returns

CilMethodBody

The single matching item.

Exceptions

MemberNotFoundException

The method, accessor, or CIL body is missing, or the method match is ambiguous.

GetEvent(TypeSpec, EventSpec)

Returns exactly one matching event from the uniquely matched type.

public EventDefinition GetEvent(TypeSpec type, EventSpec ev)

Parameters

type TypeSpec

The type specification used to select candidate types.

ev EventSpec

The event specification used to select candidate events.

Returns

EventDefinition

The single matching item.

Exceptions

MemberNotFoundException

The type or event is missing, or the event match is ambiguous.

GetField(TypeSpec, FieldSpec)

Returns exactly one matching field from the uniquely matched type.

public FieldDefinition GetField(TypeSpec type, FieldSpec field)

Parameters

type TypeSpec

The type specification used to select candidate types.

field FieldSpec

The field specification used to select candidate fields.

Returns

FieldDefinition

The single matching item.

Exceptions

MemberNotFoundException

The type or field is missing, or the field match is ambiguous.

GetInstruction(TypeSpec, MethodSpec, InstructionSpec)

Returns exactly one matching instruction from the uniquely matched method body.

public InstructionMatch GetInstruction(TypeSpec type, MethodSpec method, InstructionSpec instruction)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

instruction InstructionSpec

The instruction specification used to select candidate instructions.

Returns

InstructionMatch

The single matching item.

Exceptions

MemberNotFoundException

The method, body, or instruction is missing, or the instruction match is ambiguous.

GetLocal(TypeSpec, MethodSpec, LocalSpec)

Returns exactly one local variable from the uniquely matched method body.

public CilLocalVariable GetLocal(TypeSpec type, MethodSpec method, LocalSpec local)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

local LocalSpec

The local-variable specification used to select candidate locals.

Returns

CilLocalVariable

The single matching item.

Exceptions

MemberNotFoundException

The method, body, or local variable is missing, or the local match is ambiguous.

GetMethod(TypeSpec, EventSpec, EventAccessor)

Gets the requested add, remove, or fire method of an event.

public MethodDefinition GetMethod(TypeSpec type, EventSpec ev, EventAccessor accessor)

Parameters

type TypeSpec

The type specification used to select candidate types.

ev EventSpec

The event specification used to select candidate events.

accessor EventAccessor

The property or event accessor to retrieve.

Returns

MethodDefinition

The single matching item.

Exceptions

MemberNotFoundException

The event or requested accessor is missing.

GetMethod(TypeSpec, MethodSpec)

Returns exactly one method matching the supplied type and method specifications.

public MethodDefinition GetMethod(TypeSpec type, MethodSpec method)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

Returns

MethodDefinition

The single matching item.

Exceptions

MemberNotFoundException

The type or method is missing, or the method match is ambiguous.

GetMethod(TypeSpec, PropertySpec, PropertyAccessor)

Gets the requested getter or setter method of a property.

public MethodDefinition GetMethod(TypeSpec type, PropertySpec property, PropertyAccessor accessor)

Parameters

type TypeSpec

The type specification used to select candidate types.

property PropertySpec

The property specification used to select candidate properties.

accessor PropertyAccessor

The property or event accessor to retrieve.

Returns

MethodDefinition

The single matching item.

Exceptions

MemberNotFoundException

The property or requested accessor is missing.

GetParameter(TypeSpec, MethodSpec, ParameterSpec)

Returns exactly one matching parameter from the uniquely matched method.

public Parameter GetParameter(TypeSpec type, MethodSpec method, ParameterSpec parameter)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

parameter ParameterSpec

The parameter specification used to select candidate parameters.

Returns

Parameter

The single matching item.

Exceptions

MemberNotFoundException

The method or parameter is missing, or the parameter match is ambiguous.

GetPattern(TypeSpec, MethodSpec, params InstructionSpec[])

Returns exactly one contiguous instruction sequence matching the ordered pattern.

public InstructionSequence GetPattern(TypeSpec type, MethodSpec method, params InstructionSpec[] pattern)

Parameters

type TypeSpec

The type specification used to select candidate types.

method MethodSpec

The method specification used to select candidate methods.

pattern InstructionSpec[]

The ordered instruction specifications that form the pattern.

Returns

InstructionSequence

The single matching item.

Exceptions

MemberNotFoundException

No sequence matches, or more than one sequence matches.

ArgumentException

The pattern is empty.

GetProperty(TypeSpec, PropertySpec)

Returns exactly one matching property from the uniquely matched type.

public PropertyDefinition GetProperty(TypeSpec type, PropertySpec property)

Parameters

type TypeSpec

The type specification used to select candidate types.

property PropertySpec

The property specification used to select candidate properties.

Returns

PropertyDefinition

The single matching item.

Exceptions

MemberNotFoundException

The type or property is missing, or the property match is ambiguous.

GetType(TypeSpec)

Returns the unique type matching the supplied specification.

public TypeDefinition GetType(TypeSpec type)

Parameters

type TypeSpec

The type specification used to select candidate types.

Returns

TypeDefinition

The single matching item.

Exceptions

MemberNotFoundException

No type matches, or more than one type matches.

Refresh()

Invalidates the cached type list after types are added to or removed from the underlying module.

public void Refresh()

Save(string)

Writes the underlying module to the specified output path.

public void Save(string path)

Parameters

path string

The output file path.

Remarks

Write to a different path than the one the module was loaded from: the input file may still be in use by the loaded module, which can make overwriting it fail.