tags:

views:

50

answers:

4

Hi,

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 !

A: 

sorry, based on what I know, C# is a compiled language. I dont thing youre going to get away with executing code without a compiler.

you may want to consider a scripting language or a dynamic language.

DevilDog74
Thank you for your quick answer. In fact I would like to be able to generate CLR from existing host C# code, this last one being compiled. I was guessing if there are any CLR methods which allow to load CIL code at runtime.
Antoine Trouve
-1 because you can generate and compile code at runtime using various APIs. The CLR already dynamically compiles the intermediate bytecode to native, so even the "compiled language" part isn't strictly true.
siride
@siride not really sure why you voted my answer down. i never said that you could not generate and compile code at runtime, infact, i said that "c# is a compiled language, and you need a compiler." even if you stored IL code in a string, it would still need to be compiled before it was executed. is that incorrect?
DevilDog74
Yes and no. You can actually emit IL bytecode at runtime directly without a compiler or assembler. But doing the same for C# code is less than trivial. Dynamic languages have to do the same thing, but the gory details are hidden away behind something like eval().
siride
A: 

I may not understand the question, but couldn't you, instead of generating C# code from your application, actually generate the equivalent intermediate language (IL) code that the C# compiler would generate anyway? Then you could just run the IL code directly without having to compile it first.

Joel Marcey
You're right, so my question was about how to do that. Sorry for not being clear enough.
Antoine Trouve
+2  A: 

If you just want to create .Net functions, Expression trees are the way to go. If you want to make a whole class, you can use Reflection.Emit, but then you're writing assembly code.

If you wait for .Net 5, maybe they will have a version of the compiler that you can pass arbitrary C# code to, but for now maybe you can use something that uses the DLR (like IronPython) to pass actual source code to.

Gabe
I did not known about expression trees; those are better than the CODEDOM which I used some time ago. But I was looking for a more low-level solution.
Antoine Trouve
+2  A: 

I suspect that System.Reflection.Emit is 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...

Jon Skeet
Thank you for you answer. I took a look at this namspace, and particulary to the ILGenerator which seems to be what I was looking for. I'll look at it in more details, especially regarding the speed.
Antoine Trouve