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
-~----------~----~----~----~------~----~------~--~---