Short question,
Is there a way in .NET 4.0 to take a string that represents the method body, and compile it into a Func/Action, or is there a library to do so?
Clarification:
I need something that will not generate any dll, it needs to be completely dynamic, something like eval() in javascript. I need to convert string into a Func/Action without creating dll.
You can use the
CSharpCodeProviderclass to compile source code into an assembly.For example:
To compile a single function, you can wrap it in a class with appropriate
usingstatements to create a complete source file, then get a delegate using Reflection:For a more complete example: