简体   繁体   中英

Programmatically call C# Compiler to Compile C# code

I have designed my very own language that I, in the end, translate to C# code. Now I want to compile this C# code using the C# compiler through my Windows Forms Application.

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 . http://support.microsoft.com/kb/304655 , or MSDN have examples.

You can use C# CodeDom for this purpose. This link may help you

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 .

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

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

and

2 - Determining Which Version of the .NET Framework Is Installed

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:

"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."

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

Have you considered using Emit instead?

The csc.exe compiler is part of the .Net :

The csc.exe executable is usually located in the Microsoft.NET\\Framework\\ folder under the system directory. 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. For more information about such installations, see Determining Which Version of the .NET Framework Is Installed .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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