I know that you can dynamically create a .NET assembly using Emit, System.Reflection and manually created IL code, as shown here .
But I was wondering if it is possible to dynamically create and execute C # code in real time in a running application. Thanks for any input or ideas.
Edit: As far as I understand, CodeDOM allows you to compile C # code into an EXE file, and not "just" execute it. Here is some background information and why (as far as I can tell) this is not the best option for me. I am creating an application that will have to execute such dynamically generated code quite a lot [for writing, this is for academic research, and not for a real application, so this cannot be avoided). Therefore, creating / executing thousands of dynamically created EXE files is inefficient. Secondly, all fragments of dynamic code return some data that is difficult to read from a separately working EXE. Please let me know if I missed something.
As for the DynamicMethod approach John Skeet pointed out, everything will work as a charm if there is an easier way to write the code itself, rather than a low-level IL code.
In other words (very roughly) I need something like this:
string x = "_some c# code here_"; var result = Exec(x);
rexem source share