开发者

Execute a user-provided string containing c# code [duplicate]

This question already has answers here: Closed 11 years ago.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜