简体   繁体   English

如何允许我的.net程序在运行时编译C#代码?

[英]How do I allow my .net program to compile C# code while running?

I've seen a couple .net applications that allow you to select a C# .cs source file and the program itself will compile this code and run it. 我见过一些允许你选择C#.cs源文件的.net应用程序,程序本身将编译这段代码并运行它。

How is this done? 这是怎么做到的?

It probably uses the CSharpCodeProvider class in the Microsoft.CSharp namespace. 它可能使用Microsoft.CSharp命名空间中的CSharpCodeProvider类。 You would want to look at the following methods to compile code: 您可能希望查看以下方法来编译代码:

CompileAssemblyFromDom
CompileAssemblyFromFile
CompileAssemblyFromSource

CodeDOM是你的朋友

MSDN has a great series of articles explaining this... MSDN有很多文章解释这个......

http://msdn.microsoft.com/en-us/library/650ax5cx.aspx http://msdn.microsoft.com/en-us/library/650ax5cx.aspx

My guess is by explicitly calling the csc.exe compiler. 我的猜测是通过显式调用csc.exe编译器。 Keep in mind that this and any dependencies would have to be included with your deployment. 请记住,部署中必须包含此依赖项和任何依赖项。

You can take a look at CS-Script which interprets C# files. 您可以查看解释C#文件的CS-Script Its free but not open sources though... 它的免费但不公开的来源虽然......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM