Microsoft.CSharp.CSharpCodeProvider
Microsoft.CSharp.CSharpCodeProvider
MSDN
提供對(duì) C# 代碼生成器和代碼編譯器的實(shí)例的訪問。類提供可用來(lái)檢索 C#?ICodeGenerator?和?ICodeCompiler?實(shí)現(xiàn)的實(shí)例的方法。
下面的示例使用 C# 或 Visual Basic 代碼提供程序編譯源文件。該示例檢查輸入文件擴(kuò)展名并使用相應(yīng)的?CSharpCodeProvider?或?VBCodeProvider?進(jìn)行編譯。輸入文件被編譯為可執(zhí)行文件,并會(huì)在控制臺(tái)上顯示所有編譯錯(cuò)誤。
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中的動(dòng)態(tài)編譯
動(dòng)態(tài)源代碼生成和編譯(MSDN)
生成源代碼和在 CodeDOM 圖中編譯程序(MSDN)
一些重要的信息如下:
使用 CodeDOM 代碼提供程序編譯程序集
調(diào)用編譯
若要使用 CodeDom 提供程序編譯程序集,必須有要用某種有編譯器的語(yǔ)言編譯的源代碼,或者有 CodeDOM 圖(可用來(lái)生成要編譯的源代碼)。
如果從 CodeDOM 圖進(jìn)行編譯,請(qǐng)將包含該圖的?CodeCompileUnit?傳遞給代碼提供程序的?CompileAssemblyFromDom?方法。如果您具有使用編譯器可以理解的語(yǔ)言編寫的源代碼文件,請(qǐng)將包含源代碼的文件的名稱傳遞給 CodeDom 提供程序的?CompileAssemblyFromFile?方法。也可以將包含用編譯器識(shí)別的語(yǔ)言編寫的源代碼的字符串傳遞給 CodeDom 提供程序的CompileAssemblyFromSource?方法。
配置編譯參數(shù)
CodeDom 提供程序的所有標(biāo)準(zhǔn)編譯調(diào)用方法都有一個(gè)?CompilerParameters?類型的參數(shù),該參數(shù)指示用于編譯的選項(xiàng)。
可以在?CompilerParameters?的?OutputAssembly?屬性中指定輸出程序集的文件名。否則,將使用默認(rèn)的輸出文件名。
默認(rèn)情況下,新的?CompilerParameters?在初始化時(shí),其?GenerateExecutable?屬性被設(shè)置為?false。如果編譯可執(zhí)行程序,必須將?GenerateExecutable?屬性設(shè)置為?true。當(dāng)GenerateExecutable?設(shè)置為?false?時(shí),編譯器將生成一個(gè)類庫(kù)。
如果從 CodeDOM 圖編譯可執(zhí)行程序,必須在圖中定義一個(gè)?CodeEntryPointMethod。如果有多個(gè)代碼入口點(diǎn),可能需要將?CompilerParameters?的?MainClass?屬性設(shè)置為定義要使用的入口點(diǎn)的類名。
要將調(diào)試信息包含在生成的可執(zhí)行程序中,請(qǐng)將?IncludeDebugInformation?屬性設(shè)置為?true。
如果您的項(xiàng)目引用了任何程序集,必須將作為?StringCollection?中的項(xiàng)的程序集名稱指定為調(diào)用編譯時(shí)使用的?CompilerParameters?的?ReferencedAssemblies?屬性。
通過將?GenerateInMemory?屬性設(shè)置為?true,可以編譯寫入內(nèi)存而不是磁盤的程序集。當(dāng)在內(nèi)存中生成程序集時(shí),代碼可從?CompilerResults?的?CompiledAssembly?屬性中獲取生成的程序集的引用。如果將程序集寫入磁盤,可從?CompilerResults?的?PathToAssembly?屬性中獲取生成的程序集的路徑。
要指定在調(diào)用編譯進(jìn)程時(shí)使用的自定義命令行參數(shù)字符串,請(qǐng)?jiān)?CompilerOptions?屬性中設(shè)置該字符串。
如果調(diào)用編譯器進(jìn)程時(shí)必須使用 Win32 安全標(biāo)記,請(qǐng)?jiān)?UserToken?屬性中指定該標(biāo)記。
要將 Win32 資源文件鏈接到編譯的程序集中,請(qǐng)?jiān)?Win32Resource?屬性中指定 Win32 資源文件的名稱。
要指定暫停編譯的警告等級(jí),請(qǐng)將?WarningLevel?屬性設(shè)置為一個(gè)表示暫停編譯的警告等級(jí)的整數(shù)。也可以通過將?TreatWarningsAsErrors?屬性設(shè)置為?true,配置編譯器在遇到警告時(shí)暫停編譯。
總結(jié)
以上是生活随笔為你收集整理的Microsoft.CSharp.CSharpCodeProvider的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用两个堆栈来实现队列
- 下一篇: 如何在openbsd 5.1-5.3上部