I hold body of method in string. I want to create method dynamically. But I don't know, how to set its body. I saw very tedious way using CodeDom. And I saw using Emit with OpCodes. Is any way to use ready code from string variable?
string method_body = "return \"Hello, world!\";"; //there is method body
DynamicMethod dm = new System.Reflection.Emit.DynamicMethod("My_method",
typeof(string), new Type[] { }); //any way to create method dynamically
//any way to set body
string result = (string)dm.Invoke(...); //I need write result in variable
Save this to a .CS file and compile and execute it on the fly.
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:
CSharpCodeProvider
, but you'll have to load it in as a method (and create a delegate to it) via reflectionCodeDom
Reflection.Emit
Expression
In 4.0, the
Expression
API is far richer than it was in 3.5, allowing most common constructs without the pain ofCodeDom
. But don't discountReflection.Emit
- it takes a little while to get your head aroundILGenerator
and using the stack, but it isn't as bad as people tend to think.As a side-note, don't use
Invoke
fromDynamicMethod
unless you only want to execute it once. The better approach is to useCreateDelegate
, then store (and re-use) that delegate:Or with the
Expression
API:You need to take a look at these namespaces:
This may get you started: http://www.west-wind.com/presentations/dynamicCode/DynamicCode.htm