I have a dynamic method and I have the byte[] from the real method (using Cecil). Now how to assign this byte array to the dynamic method and execute it? I'm sure it's not only one way thing, there must be a way to invoke byte arrays.
-
save it and run it. You can try using unsafe code too.j. – Alvin Wong Aug 20 '12 at 14:14
-
1@AlvinWong by itself, I'm not sure that is hugely helpful... – Marc Gravell Aug 20 '12 at 14:16
-
Is it just the CLR for a single method, or is it actually an entire assembly? – PhonicUK Aug 20 '12 at 14:18
-
So I can only tell it's not that easy. There had been some questions of running machine code in memory by C/C++. Given the managed nature of C# it can be much difficult or even impossible? – Alvin Wong Aug 20 '12 at 14:20
-
Have you tried `Assembly.Load` + `Reflection` ? – L.B Aug 20 '12 at 14:23
-
2If this is a *method*, then I'm thinking something involving `DynamicMethod`'s `GetDynamicILInfo()` method, then the `SetCode()` method. However, there are no examples on MSDN to look at. – Marc Gravell Aug 20 '12 at 14:24
-
@MarcGravell: This could be it. Thanks. – blez Aug 20 '12 at 14:26
-
@blez I couldn't get it working locally, or I would have posted it as an answer ;p – Marc Gravell Aug 20 '12 at 14:27
4 Answers
Possible with a little known static method:
Some conditions apply, but should do exactly what you need :) See the example.

- 115,091
- 17
- 196
- 297
First, why don't you simply invoke the method without doing any copying? That should be much simpler than what you're trying to do.
Basically, you can't do that. That's because the byte[]
contains metadata tokens that reference things like other assemblies or methods. And these tokens are different in each assembly, even if they represent the same thing. So, if you just tried to execute the byte[]
in another assembly, the metadata tokens would be completely wrong.
You could parse the byte[]
into the IL instructions it represents and translate the metadata tokens for instructions which have them, but doing that properly could be a lot of work. I believe Cecil could help you with that.
Another option might be to try to copy all of the metadata tokens from the old assembly to new dynamic one, so that the tokens in the byte[]
would be valid. But I have no idea how feasible this is.
Also, if the method was really simple (no methods called and no custom types used), then simply copying the byte[]
should work.

- 236,525
- 50
- 385
- 514
You will need to use a DynamicMethod for that. Use DynamicMethod.GetDynamicILInfo
and then set the code, local variables and exception handlers. You will also need to fix any tokens that appear in your IL byte buffer. Then call using DynamicMethod.Invoke
.
For all but the most trivial methods, achieving this is not an easy task.

- 5,796
- 1
- 28
- 32
You can't execute that: Cecil is a good tool, but it's not suited for code that needs to be executed at runtime on a method-by-method basis. You should be using Reflection.Emit for that instead.
Note that the suggested SetCode() method won't work for your case, since Cecil's understanding of metadata tokens is completely different from the runtime's, so the runtime will have no way to resolve the tokens (for example for method calls).

- 3,963
- 1
- 18
- 13
-
I don't think Reflection.Emit will work well either. Metadata tokens simply can't be easily moved from assembly to assembly. – svick Aug 21 '12 at 09:25
-
Reflection.Emit has its own token management facilities, so there is no problem there, "moving tokens between assemblies" makes no sense in this context. The user should just use Reflection.Emit to create the method to be executed at runtime and not use Cecil or other means to create byte code. – lupus Aug 22 '12 at 07:19
-
I don't know about any “token management facilities” that would work on `byte[]`, which is what the question was about. – svick Aug 22 '12 at 07:21