4

I know you can dynamically create a .NET assembly using Emit, System.Reflection and manually created IL code as shown here.

But I was wondering is it possible to dynamically create and execute C# code block real-time, in a running application. Thanks for any input or ideas.

Edit: As I understand CodeDOM allows you to compile C# code into EXE file rather than "just" executing it. Here's some background information and why (as far as I can tell) this isn't the best option for me. I'm creating an application that will have to execute such a dynamically created code quite a lot [for the record - it's for academic research, not a real-world application, thus this cannot be avoided]. Therefore, creating/executing thousands of dynamically created EXEs aren't really efficient. Secondly - all dynamic code fragments returns some data that is hard to read from separately running EXE. Please let me know if I'm missing something.

As for DynamicMethod approach, pointed out by Jon Skeet, everything would work like a charm if there would be an easier way to write the code itself rather than low level IL code.

In other words (very harshly speaking) I need something like this:

string x = "_some c# code here_";
var result = Exec(x);
Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
rexem
  • 109
  • 2
  • 8
  • 1
    Define "real-time". .NET is not a real-time platform as you don't have control over when threads will be interrupted for other tasks including garbage collection. – Eric J. Apr 19 '11 at 15:09
  • possible duplicate of [Is it possible to dynamically compile and execute C# code fragments?](http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-code-fragments) – BrokenGlass Apr 19 '11 at 15:11
  • @Eric J: Sorry, I probably misused the term "real-time". All I meant that C# code should be generated dynamically while application is running and not statically implemented before compiling it. (e.g. c# code that should be executed is dynamically read from a text stream) – rexem Apr 19 '11 at 15:53
  • Yes - see [here](http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-code-fragments). – BrandonZeider Apr 19 '11 at 15:09

4 Answers4

8

Absolutely - that's exactly what I do for Snippy for example, for C# in Depth. You can download the source code here - it uses CSharpCodeProvider.

There's also the possibility of building expression trees and then compiling them into delegates, using DynamicMethod, or the DLR in .NET 4... all kinds of things.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Yes, it is. There are several applications that do just that - see LinqPad and Snippy.

I believe they use the CSharpCodeProvider.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
0

Yes. See this MSDN page regarding using the CodeDOM.

Some example code extracted from the above mention page:

CodeEntryPointMethod start = new CodeEntryPointMethod();
CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression( 
    new CodeTypeReferenceExpression("System.Console"), 
    "WriteLine", new CodePrimitiveExpression("Hello World!") );
start.Statements.Add(cs1); 
Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
0

You can Use CodeDom to generate code and create in memory assemblies based on that generated code. THose can then be used in the current application.

Here's a quick link to the msdn reference, it is quite extensive material.

MSDN: Using the CodeDom

Anton
  • 5,323
  • 1
  • 22
  • 25