I have a C# application which generates .NET functions at run-time and executes them.
To do so, it generates C# in strings and calls the online C# compiler to convert it into CLR to be JIT compiled and executed.
For performance reasons, I would like to be able to generate directly CLR (in strings or through an internal representation) and execute it without using any compiler (but perhaps the JIT compiler).
Is that possible ? I’ve seen some articles about code injection, but that doesn’t exactly fits the bill.
Thank you !
I suspect that
System.Reflection.Emitis what you’re after. In particular, if you only want to generate a single method, I believe you should look at DynamicMethod. You can ask it to create an ILGenerator, build the IL, and then it will be JIT compiled when you convert the method into a delegate.I don’t believe you can just give it textual IL though… you’ll be using the API, explicitly building various instructions. There may be an IL parser which uses this though…