Table of Contents

Class Injector

Namespace
Embedix.Core.dnlib.Injection
Assembly
Embedix.Core.dll

Injects (deep-copies) dnlib.DotNet.TypeDefs - and everything they transitively reference from their own source module - into a different target dnlib.DotNet.ModuleDef.

public class Injector
Inheritance
Injector
Inherited Members

Remarks

This is a thin orchestration facade. The actual work is split across:

  • Embedix.Core.dnlib.Injection.InjectMapper - caches imports and resolves source-to-target references.
  • Embedix.Core.dnlib.Injection.MemberCloner - creates empty skeleton copies of definitions.
  • Embedix.Core.dnlib.Injection.TypeInitializer - builds skeletons (phase 1) and fills them in (phase 2).
  • Embedix.Core.dnlib.Injection.MemberCopier / Embedix.Core.dnlib.Injection.MethodBodyCopier - the phase-2 filling-in logic.
  • Embedix.Core.dnlib.Injection.ReferenceFixer - defensive final pass after injection.

Constructors

Injector(ModuleDef)

Initializes a new instance of the Injector class.

public Injector(ModuleDef target)

Parameters

target ModuleDef

The module that receives injected types.

Methods

Inject(IEnumerable<TypeDef>)

Injects a batch of top-level types together. Use this instead of calling Inject(TypeDef) once per type whenever the types being injected reference each other, regardless of which order they're given in - every skeleton in the whole batch is built before any body is copied.

public void Inject(IEnumerable<TypeDef> sources)

Parameters

sources IEnumerable<TypeDef>

The top-level types to inject together; null entries and already injected types are skipped.

Exceptions

ArgumentNullException

sources is null.

InjectionException

Injection failed; see InnerException.

Inject(TypeDef)

Injects source (and anything it references from its own module) into the target module. Does nothing if the type was already injected through this Injector instance.

public void Inject(TypeDef source)

Parameters

source TypeDef

The type to inject.

Remarks

Correctly resolves references between source and its own nested types regardless of which one happens to be processed first (see Embedix.Core.dnlib.Injection.TypeInitializer.BuildSkeletonTree(dnlib.DotNet.TypeDef)). If you are injecting several separate top-level types that reference each other, use Inject(IEnumerable<TypeDef>) instead so all of them get their skeletons built before any of their bodies are copied - calling this overload once per type cannot guarantee that ordering across different calls.

Exceptions

ArgumentNullException

source is null.

InjectionException

Injection failed; see InnerException.

TryInject(TypeDef, out TypeDef)

Non-throwing version of Inject(TypeDef).

public bool TryInject(TypeDef source, out TypeDef target)

Parameters

source TypeDef

The type to inject.

target TypeDef

The injected copy on success; otherwise null.

Returns

bool

true if the type was injected; false if it was already injected or injection failed.

Remarks

Does not run Embedix.Core.dnlib.Injection.ReferenceFixer.FixReferences(dnlib.DotNet.TypeDef) / Embedix.Core.dnlib.Injection.ReferenceFixer.FixGenericParameters(dnlib.DotNet.TypeDef), so references to types injected earlier through a different call may still point at the source module afterwards. References within source's own nested-type tree are still resolved correctly thanks to the two-phase build. Prefer Inject(TypeDef) unless you have a specific reason to avoid exceptions.

Exceptions

ArgumentNullException

source is null.