Directly Inserting Bytecode

.net, c#, reflection

Solution

Yes, you can use `DynamicMethod`s which basically do exactly this (you get a Delegate to invoke the code that you've generated with the dynamic method).

In fact, several framework parts seem to use them, including the compiled XSL transformations, the compiled Regular Expressions etc. They work very well and they are pretty performant (after the initial overhead for creating/compiling them).

Problem

Is there a way to dynamically insert bytecode directly into my C# executable? I'm looking for functionality similar to the asm keyword in C++. Obviously I know I can't insert x86 assembly instructions. I'd only be able insert IL Bytecode.

Original source