3

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)??

atastrophic
  • 3,153
  • 3
  • 31
  • 50

6 Answers6

5

It sounds like you are looking for the CSharpCodeProvider. http://support.microsoft.com/kb/304655, or MSDN have examples.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
3

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

Anuraj
  • 18,859
  • 7
  • 53
  • 79
0
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);
leppie
  • 115,091
  • 17
  • 196
  • 297
Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • **(comments removed)** I've removed some unhelpful comments; basically, some people have observed that it may not be necessary or appropriate to shell to `csc` to achieve this. It is a *valid* answer (assuming the paths are correct for the machine, etc), but it has been argued that there may be more direct ways to achieve this. `` – Marc Gravell Jul 16 '12 at 10:16
0

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

Carlos Quintanilla
  • 12,937
  • 3
  • 22
  • 25
0

just use nemerle compiler as service with csharp syntax.

idiot57
  • 11
-1

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.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569