0

Currently I am working on a sort of .NET assemblies protector/patcher.

The problem is, after some sort of dynamic code injection (performed via MethodBody.ILProcessor.Emit() family of routines) the newly added types, methods and other artifacts, taken from the already compiled templates assembly, are to be placed in the target assembly. So, after injection, there is the target assembly modified, with no references to the template assembly. After such operations (types, methods, etc copying) the metadata tokens of the injected artifacts (types, methods, etc) are invalid (i.e., all MethodDefinition-s from the template assembly, on being copied to the target assembly, recieve the metadata tokens, corresponding to the MethodDefinition-s with RID == 0). But trying to save the target assembly bring no effect, as it is already utilized, and hence, locked for writing, despite the target assembly is loaded as:
AssemblyDefinition target = AssemblyDefinition.ReadAssembly(path, new ReaderParameters(ReadingMode.Immediate));

So, my question is: Is there any way (in Mono.Cecil) to make all metadata tokens update hierarchally (i.e., receive such RIDs that RID != 0), without actually writing the file? Or at least to assign/distribute metadata tokens manually?

P.S. Thanks in advance!

1 Answer 1

0

Basically I could not find such a possibility. As I was using the Mono.Cecil library, it all came to a point that after sort of code manipulation some previously refered classes (in my specific case, a System.UInt64) disappered from the 'test' assembly (since Mono.Cecil removes unused references, and I was not using it properly). Well, the point is, once again, is that Mono.Cecil removes unused references when the assembly edited is being saved, so do not rely on outdated references once you have save the assembly and want to mutate it later (e.g., during execution).

Not the answer you're looking for? Browse other questions tagged or ask your own question.