Microsoft.CSharp.CSharpCodeProvider
提供對 C# 代碼生成器和代碼編譯器的實例的訪問。類提供可用來檢索 C# ICodeGenerator 和 ICodeCompiler 實現的實例的方法。
下面的示例使用 C# 或 Visual Basic 代碼提供程序編譯源文件。該示例檢查輸入文件擴展名並使用相應的 CSharpCodeProvider 或 VBCodeProvider 進行編譯。輸入文件被編譯為可執行文件,並會在控制台上顯示所有編譯錯誤。
public static bool CompileExecutable(String sourceName) { FileInfo sourceFile = new FileInfo(sourceName); CodeDomProvider provider = null; bool compileOk = false; // Select the code provider based on the input file extension. if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".CS") { provider = new Microsoft.CSharp.CSharpCodeProvider(); } else if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".VB") { provider = new Microsoft.VisualBasic.VBCodeProvider(); } else { Console.WriteLine("Source file must have a .cs or .vb extension"); } if (provider != null) { // Format the executable file name. // Build the output assembly path using the current directory // and <source>_cs.exe or <source>_vb.exe. String exeName = String.Format(@"{0}\{1}.exe", System.Environment.CurrentDirectory, sourceFile.Name.Replace(".", "_")); CompilerParameters cp = new CompilerParameters(); // Generate an executable instead of // a class library. cp.GenerateExecutable = true; // Specify the assembly file name to generate. cp.OutputAssembly = exeName; // Save the assembly as a physical file. cp.GenerateInMemory = false; // Set whether to treat all warnings as errors. cp.TreatWarningsAsErrors = false; // Invoke compilation of the source file. CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceName); if(cr.Errors.Count > 0) { // Display compilation errors. Console.WriteLine("Errors building {0} into {1}", sourceName, cr.PathToAssembly); foreach(CompilerError ce in cr.Errors) { Console.WriteLine(" {0}", ce.ToString()); Console.WriteLine(); } } else { // Display a successful compilation message. Console.WriteLine("Source {0} built into {1} successfully.", sourceName, cr.PathToAssembly); } // Return the results of the compilation. if (cr.Errors.Count > 0) { compileOk = false; } else { compileOk = true; } } return compileOk; }
以下文檔可供參考:
.NET中的動態編譯
動態源代碼生成和編譯(MSDN)
生成源代碼和在 CodeDOM 圖中編譯程序(MSDN)
一些重要的信息如下:
使用 CodeDOM 代碼提供程序編譯程序集
調用編譯
若要使用 CodeDom 提供程序編譯程序集,必須有要用某種有編譯器的語言編譯的源代碼,或者有 CodeDOM 圖(可用來生成要編譯的源代碼)。
如果從 CodeDOM 圖進行編譯,請將包含該圖的 CodeCompileUnit 傳遞給代碼提供程序的 CompileAssemblyFromDom 方法。如果您具有使用編譯器可以理解的語言編寫的源代碼文件,請將包含源代碼的文件的名稱傳遞給 CodeDom 提供程序的 CompileAssemblyFromFile 方法。也可以將包含用編譯器識別的語言編寫的源代碼的字符串傳遞給 CodeDom 提供程序的CompileAssemblyFromSource 方法。
配置編譯參數
CodeDom 提供程序的所有標准編譯調用方法都有一個 CompilerParameters 類型的參數,該參數指示用於編譯的選項。
可以在 CompilerParameters 的 OutputAssembly 屬性中指定輸出程序集的文件名。否則,將使用默認的輸出文件名。
默認情況下,新的 CompilerParameters 在初始化時,其 GenerateExecutable 屬性被設置為 false。如果編譯可執行程序,必須將 GenerateExecutable 屬性設置為 true。當GenerateExecutable 設置為 false 時,編譯器將生成一個類庫。
如果從 CodeDOM 圖編譯可執行程序,必須在圖中定義一個 CodeEntryPointMethod。如果有多個代碼入口點,可能需要將 CompilerParameters 的 MainClass 屬性設置為定義要使用的入口點的類名。
要將調試信息包含在生成的可執行程序中,請將 IncludeDebugInformation 屬性設置為 true。
如果您的項目引用了任何程序集,必須將作為 StringCollection 中的項的程序集名稱指定為調用編譯時使用的 CompilerParameters 的 ReferencedAssemblies 屬性。
通過將 GenerateInMemory 屬性設置為 true,可以編譯寫入內存而不是磁盤的程序集。當在內存中生成程序集時,代碼可從 CompilerResults 的 CompiledAssembly 屬性中獲取生成的程序集的引用。如果將程序集寫入磁盤,可從 CompilerResults 的 PathToAssembly 屬性中獲取生成的程序集的路徑。
要指定在調用編譯進程時使用的自定義命令行參數字符串,請在 CompilerOptions 屬性中設置該字符串。
如果調用編譯器進程時必須使用 Win32 安全標記,請在 UserToken 屬性中指定該標記。
要將 Win32 資源文件鏈接到編譯的程序集中,請在 Win32Resource 屬性中指定 Win32 資源文件的名稱。
要指定暫停編譯的警告等級,請將 WarningLevel 屬性設置為一個表示暫停編譯的警告等級的整數。也可以通過將 TreatWarningsAsErrors 屬性設置為 true,配置編譯器在遇到警告時暫停編譯。