[Mono-dev] Method code injection

Amir Ebrahimi amir at unity3d.com
Tue Mar 16 11:47:17 EDT 2010


Thanks, Rodrigo. I'll try this out!

On Sat, Mar 13, 2010 at 6:14 AM, Rodrigo B. de Oliveira <
rodrigobamboo at gmail.com> wrote:

> Hey Amir!
>
> Yes, you can use Mono.Cecil:
>
>    import System
>    import Mono.Cecil
>    import Mono.Cecil.Cil
>
>    pathToAssembly = "/tmp/test.exe"
>    asm = AssemblyFactory.GetAssembly(pathToAssembly)
>
>    module = asm.MainModule
>    method = module.Types["Program"].Methods.GetMethod("Main")[0]
>
>    worker = method.Body.CilWorker
>    firstInstruction = method.Body.Instructions[0]
>    worker.InsertBefore(firstInstruction, worker.Create(OpCodes.Ldstr,
> "Hello!"))
>    worker.InsertBefore(firstInstruction, worker.Create(OpCodes.Call,
> module.Import(typeof(Console).GetMethod("WriteLine", (string,)))))
>
>    AssemblyFactory.SaveAssembly(asm, pathToAssembly)
>    AppDomain.CurrentDomain.ExecuteAssembly(pathToAssembly)
>



-- 
Amir Ebrahimi
Technical Account Manager
Unity Technologies :: http://unity3d.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20100316/04041cad/attachment.html 


More information about the Mono-devel-list mailing list