4

I would like to compile c# source code from a string. I know this is possible using CodeDom, and I would like to know if it's possible to do this using the command line compiler. For example, say I load the following code onto a string:

static void Main(string[] args)
        {           

        }

Is there some way to compile this code into a c# executable (.exe) file without first writing this code to my hard drive in the form of a C# class (.cs) file?

That was the first part of my question. The second part assumes that the above is in fact impossible. Let's say I had three difference classes (loaded onto three separate strings). How would I go about using the Command line compiler to compile each of these three classes into one common executable file? I have found some examples online, but they seem to be very vague, unfortunately.

Any help, or pointers in the right direction is much appreciated.

Thank you, Evan

  • Why do you want to use the command line compiler instead of the compiler classes? – CodesInChaos Aug 15 '11 at 00:07
  • I don't want to use CodeDom because it has been causing a few bugs lately. Also, I'm slightly curious at this point. –  Aug 15 '11 at 00:07
  • Use CodeCompiler.FromSource(). It takes a string. – Hans Passant Aug 15 '11 at 00:09
  • @Hans - is this not part of CodeDom? I want to use CSC.exe –  Aug 15 '11 at 00:12
  • Color me surprised, it is the System.CodeDom.CodeCompiler class. There has to be a real question behind that comment, I can't see it yet. – Hans Passant Aug 15 '11 at 00:14
  • I stated in my question that I did not want to use CodeDom. Why the attitude? I did not mean anything by my comment. –  Aug 15 '11 at 00:18
  • Attitude does seem to be the problem, System.CodeDom was *made* to do what you want. It uses csc.exe, there's only one C# compiler. Why don't you want to use it? – Hans Passant Aug 15 '11 at 00:26
  • @Hans: I think you have that backwards: csc.exe uses CodeDOM. – Ben Voigt Aug 15 '11 at 12:55
  • @Ben Why not just make your suggestion an answer? It solved my problem... Also, don't bother with Hans, it seems no matter what you tell him he's going to have something to say about it. Wasting your breath. –  Aug 15 '11 at 12:59
  • @Evan: I added it as a clarification on my existing answer. – Ben Voigt Aug 15 '11 at 13:00

2 Answers2

3

No, you can't use csc.exe without a source file. What is your rationale for needing to use csc.exe? Have you found something that the CodeDOM doesn't handle correctly? A command-line option you don't have a CodeDOM equivalent for?

The C# team mentioned that a future version might support compiler-as-a-service, where csc.exe would just become a thin wrapper around a compiler DLL, that you could also call directly. But you'd still need to bypass csc.exe.

You can use System.CodeDom.Compiler.CompilerParameters to get CodeDOM to give you the same results as csc.exe. Check the build log to find out what options Visual Studio is passing to csc.exe. Specifically, the architecture (AnyCPU vs x86) is likely to make a big difference, since it determines whether the resulting assembly will be compatible with 32-bit DLLs when run on Windows x64. Or you can use corflags to fix the architecture after compilation.

Community
  • 1
  • 1
Ben Voigt
  • 260,885
  • 36
  • 380
  • 671
  • Yes, I have a project which uses pointers, and invokes data. For some reason when I compile this source using CodeDom, the output fails on Windows 7 64 bit. However, when I compile the EXACT source code using Visual Studio, the program works on windows 7 64 bit. –  Aug 15 '11 at 00:17
  • @Evan: Maybe one is using `AnyCPU` and the other is `x86`? Use [`corflags`](http://msdn.microsoft.com/en-us/library/ms164699.aspx) to check. – Ben Voigt Aug 15 '11 at 00:18
  • Yes, Visual Studio is using x86, CodeDom is using any CPU. Shouldn't it be the opposite though based on my findings? –  Aug 15 '11 at 00:22
  • 2
    @Evan: Nope, the `x86` one is compatible with a 32-bit DLL even on Windows x64. On the other hand, `AnyCPU` loads as a 64-bit process on Windows x64, and then the 32-bit DLL cannot be used. So now you just need to find the `x86` option for CodeDOM, or run `corflags` on the resulting executable. – Ben Voigt Aug 15 '11 at 00:23
  • If you made this an answer I would accept it. Just solved my issue! –  Aug 15 '11 at 01:20
2

I was looking to see if you could direct CSC to compile from STDIN but I can't seem to see that, so I think Part one isn't likely to be the case.

However, part two is easier.

a command such as

csc /out:test.exe *.cs

Compiles all .CS files in the current directory into test.exe. You can find other examples here:

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

Edit: I just thought of a horrible hack that might help. This is horrible however! What about creating your file as some kind of UNC/Service url. My thinking is that you could associate a NetworkStream to a UNC or an endpoint of some HttpHandler or webservice; once read from you would simply return the string to the stream.

This is really hacky though and I have no idea if it'll work!

Russ Clarke
  • 16,600
  • 3
  • 36
  • 43