Ха, прикольно, не знал о такой возможности, но в MSDN она вполне нормально описана, вот что у меня получилось:
| Код | public static CompilerResults CompileCode(CodeDomProvider provider, String sourceFile, String exeFile) { // Configure a CompilerParameters that links System.dll // and produces the specified executable file. String[] referenceAssemblies = { "System.dll" }; CompilerParameters cp = new CompilerParameters(referenceAssemblies, exeFile, false); // Generate an executable rather than a DLL file. cp.GenerateExecutable = true;
// Invoke compilation. CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceFile); // Return the results of compilation. return cr; }
static void Main(string[] args) { CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
string sourceFile = "..\\test.cs"; CompilerResults cr = CompileCode(provider, sourceFile, "..\\test.exe");
if (cr.Errors.Count > 0) { // Display compilation errors. Console.WriteLine("Errors encountered while building " + sourceFile + " into " + cr.PathToAssembly + ": \r\n\n"); foreach (CompilerError ce in cr.Errors) Console.WriteLine(ce.ToString() + "\r\n"); } else { Console.WriteLine("Source " + sourceFile + " built into " + cr.PathToAssembly + " with no errors.");
} Console.ReadLine(); }
|
| Код | // test.cs using System;
namespace CSharpWelcome { class Welcome { static void Main() { Console.WriteLine("Welcome to C#!"); Console.ReadLine(); } } }
|
|