It sounds like what you want is the "compiler as a service". That is not in MS .NET 4.0, but may be in a later release. It is already in Mono, though. Until then, the options available are:
- use
CSharpCodeProvider
, but you'll have to load it in as a method (and create a delegate to it) via reflection
- use
CodeDom
- use
Reflection.Emit
- use
Expression
In 4.0, the Expression
API is far richer than it was in 3.5, allowing most common constructs without the pain of CodeDom
. But don't discount Reflection.Emit
- it takes a little while to get your head around ILGenerator
and using the stack, but it isn't as bad as people tend to think.
As a side-note, don't use Invoke
from DynamicMethod
unless you only want to execute it once. The better approach is to use CreateDelegate
, then store (and re-use) that delegate:
var dm = new System.Reflection.Emit.DynamicMethod("My_method",
typeof(string), null);
var il = dm.GetILGenerator();
il.Emit(OpCodes.Ldstr, "Hello, world!");
il.Emit(OpCodes.Ret);
Func<string> func = (Func<string>)dm.CreateDelegate(typeof(Func<string>));
var s = func();
Or with the Expression
API:
var lambda =Expression.Lambda<Func<string>>(Expression.Constant("Hello, world"));
var func = lambda.Compile();
var s = func();