Well, after many experiments, it appears that simple Clone() or Import () calls are far from enough. Basically all type references (system types or user types) need to be exhausted and imported to target AssemblyDefinition. If source Assembly resources are used by source types, those resources need to be imported and whose references need to be updated accordingly. Furthermore, BaseType, FieldType, ParamterType, VariableType, as well as instruction-level (Operand) MethodReferences, TypeReferences, and FieldDefinitions all need to be updated. Yet to find out if there are more...
For System type references, targetAssemblyDefinition.MainModule.Import () comes handy. For user types, they need to be cloned, and maybe stored in a Dictionary<OriginalTypeDefinition, ClonedTypeDefinition> for later reference, when import gets to Field / Method levels. A challenge would be in GenericParameters... Overall Mono.Cecil provides a excellent foundation for very powerful stuff! On Mar 19, 12:43 am, Mar7in <[email protected]> wrote: > After experimenting with code injection, as it takes quite a long time > to inject code manually. I decided to attempt Type injection. I looked > around Mono.Cecil and came across a few Inject methods in > ModuleDefinition.cs. I made the following attemp: > > //<Snip> > AssemblyDefinition adFrom = AssemblyFactory.GetAssembly > (fromAssemblyFile); > AssemblyDefinition adInject = AssemblyFactory.GetAssembly > (injectAssemblyFile); > > foreach (TypeDefinition td in adFrom.MainModule.Types) > { > if (td.FullName != "<Module>") > { > adInject.MainModule.Inject(td); > }} > > //</Snip> > > SaveAssembly(adInject, injectAssemblyFile); > > Well, as I thought in the first palce - "too good to be true". :) That > did not produce a valid Assembly from "adInject". I wonder what I am > doing wrong? Thanks for any pointers to achieve "Type Injection" from > another Assembly. > > P.S. I also attempted TypeDefinition cloning and method body > MethodReference modification, VariableDefinition imports. That > succeeded in injecting a basic class; but failed to produce working > target assembly when there is type inheritance involved. --~--~---------~--~----~------------~-------~--~----~ -- mono-cecil -~----------~----~----~----~------~----~------~--~---
