简体   繁体   English

以编程方式调用C#编译器编译C#代码

[英]Programmatically call C# Compiler to Compile C# code

I have designed my very own language that I, in the end, translate to C# code. 我设计了我自己的语言,最后我将其转换为C#代码。 Now I want to compile this C# code using the C# compiler through my Windows Forms Application. 现在我想通过我的Windows窗体应用程序使用C#编译器编译这个C#代码。

Where is the actual compiler file located and how do I compile my code (currently placed in a string)?? 实际的编译器文件位于何处,如何编译我的代码(当前放在一个字符串中)?

It sounds like you are looking for the CSharpCodeProvider . 听起来你正在寻找CSharpCodeProvider http://support.microsoft.com/kb/304655 , or MSDN have examples. http://support.microsoft.com/kb/304655MSDN都有示例。

You can use C# CodeDom for this purpose. 您可以使用C#CodeDom来实现此目的。 This link may help you 这个链接可以帮到你

http://www.codeproject.com/Articles/3445/Runtime-Compilation-A-NET-eval-statement http://www.codeproject.com/Articles/3445/Runtime-Compilation-A-NET-eval-statement

c:\windows\Microsoft.NET\Framework\v3.5\

contains the C# compiler csc.exe . 包含C#编译器csc.exe

So, you could do something like the following: 所以,您可以执行以下操作:

const string outputfile = "abc.exe";
const string inputFile = "xyz.cs"

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "c:\\windows\\Microsoft.NET\\Framework\v3.5\\csc.exe";
startInfo.Arguments = "/out:" + outputFile + " " + inputFile;
Process.Start(startInfo);

You need to have a look at the following 2 sites: 您需要查看以下两个站点:

1 - Command-line Building With csc.exe 1 - 使用csc.exe进行命令行构建

http://msdn.microsoft.com/en-us/library/78f4aasd.aspx http://msdn.microsoft.com/en-us/library/78f4aasd.aspx

and

2 - Determining Which Version of the .NET Framework Is Installed 2 - 确定安装了哪个版本的.NET Framework

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

You will need to identify which version of the .NET Framework compiler you will use, from the second link you can see: 您需要从第二个链接中确定要使用的.NET Framework编译器版本:

"You can install and run multiple versions of the .NET Framework on a computer. You can install the versions in any order. To see which versions are installed, view the %WINDIR%\\Microsoft.NET\\Framework directory. (You should also view the Framework64 directory on a 64-bit computer, which can have 32 or 64-bit versions installed.) Each version of the .NET Framework has a directory, and the first two digits of the directory name identify the .NET Framework version; for example: v1.1.4322 for the .NET Framework 1.1, v2.0.50727 for the .NET Framework 2.0, v3.5 for the .NET Framework 3.5, and so on." “您可以在计算机上安装和运行多个版本的.NET Framework。您可以按任何顺序安装版本。要查看安装的版本,请查看%WINDIR%\\ Microsoft.NET \\ Framework目录。(您还应该查看64位计算机上的Framework64目录,该计算机可以安装32位或64位版本。)每个版本的.NET Framework都有一个目录,目录名的前两位数字标识.NET Framework版本;例如:.NET Framework 1.1为v1.1.4322,.NET Framework 2.0为v2.0.50727,.NET Framework 3.5为v3.5,依此类推。

只需使用nemerle编译器作为csharp语法的服务。

Have you considered using Emit instead? 您是否考虑过使用Emit

The csc.exe compiler is part of the .Net : csc.exe编译器是.Net的一部分

The csc.exe executable is usually located in the Microsoft.NET\\Framework\\ folder under the system directory. csc.exe可执行文件通常位于系统目录下的Microsoft.NET \\ Framework \\文件夹中。 Its location may vary depending on the exact configuration on any individual computer. 它的位置可能会有所不同,具体取决于任何个人计 Multiple versions of this executable will be present on the computer if more than one version of the .NET Framework is installed on the computer. 如果计算机上安装了多个版本的.NET Framework,则此计算机上将存在此可执行文件的多个版本。 For more information about such installations, see Determining Which Version of the .NET Framework Is Installed . 有关此类安装的详细信息,请参阅确定安装了哪个版本的.NET Framework

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

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