Executing C# or VB.NET code at Runtime

2020-06-23 09:25发布

问题:

Been spending some time on Codility.com recently and it crossed my mind; how do they execute the code you have created (Specifically pertaining to C# and VB.NET) ?

What I am basically wondering is how would I take a textbox on a form type some code in it and then run that code? Is this possible without 3rd party tools?

Also how would you prevent security violations specifically pertaining to a web based implementation of such a program?

回答1:

Have a look at http://msdn.microsoft.com/en-us/library/system.codedom.compiler.aspx Executing such code in a web app is in deed a security problem. .Net provides a set of functionalities to help you solve that problem, like the possibility to run your compiled code in a different app domain as a different user.



回答2:

With the CSharpCodeProvider class you can compile code (as a string) during runtime.



回答3:

You can compile code in VB.Net and C# at runtime. Have a look at code to do that here