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
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.CompilerParametersto 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 (AnyCPUvsx86) 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 usecorflagsto fix the architecture after compilation.