Execute a user-provided string containing c# code [duplicate]
Possible Duplicate:
C# eval equivalent?
Hi - I was wondering how I can execute a user provided string containing c# code from my开发者_高级运维 application.
Example:
string userProvidedString = "Console.Write("Hello World!")";
ExecuteCode(userProvidedString); // Should output Hello World!
There's a CSharpCodeProvider Class in Framework that can be utilized. It contains a CompileAssemblyFromSource method that Parses and compiles the code in a String.
You might also like checking this blog post on MSDN: http://blogs.msdn.com/b/dohollan/archive/2010/08/09/programmatically-invoke-the-c-compiler.aspx
NOTE: The above blog post link does not work anymore, so use this one instead: https://learn.microsoft.com/en-us/archive/blogs/dohollan/programmatically-invoke-the-c-compiler
Follow this method: http://www.west-wind.com/presentations/dynamiccode/dynamiccode.htm
精彩评论