4

I was wondering, if it's possible to inject byte[] code that was previously exported from an method via GetMethodBody() back, meaning, that it can be runnable code again. If someone could write pretty simple example or explanation I'd appreciate.

Regards,

deadmau5
  • 95
  • 2
  • 7
  • Why would you want to do that? – svick Mar 11 '12 at 13:45
  • 2
    possible duplicate of [Creating method dynamically, and executing it](http://stackoverflow.com/questions/7671220/creating-method-dynamically-and-executing-it) – svick Mar 11 '12 at 13:49
  • I do know how to dynamically create new method. What I want to do is extracted and storted method in byte[] turn in runnable code and execute it. – deadmau5 Mar 18 '12 at 10:23
  • Have you read the answers to that question? – svick Mar 18 '12 at 10:47
  • Sorry, I thought that they were talking about something else when I read the title of the question. Thanks again svick ! – deadmau5 Mar 31 '12 at 07:47

2 Answers2

0
byte[] il = ...;
MethodBuilder mb = ...;
mb.CreateMethodBody(il, il.Length);
M.Stramm
  • 1,289
  • 15
  • 29
  • nvm, indeed is a duplicate of http://stackoverflow.com/questions/7671220/creating-method-dynamically-and-executing-it – M.Stramm Jun 28 '12 at 02:33
-1

Indeed you can. You can execute IL code using DynamicMethod.GetILGenerator(). There is an example in the link.