Microsoft.CSharp.CSharpCodeProvider

Microsoft.CSharp.CSharpCodeProvider

MSDNhtml

提供對 C# 代碼生成器和代碼編譯器的實例的訪問。類提供可用來檢索 C# ICodeGenerator 和 ICodeCompiler 實現的實例的方法。安全

下面的示例使用 C# 或 Visual Basic 代碼提供程序編譯源文件。該示例檢查輸入文件擴展名並使用相應的 CSharpCodeProvider 或 VBCodeProvider 進行編譯。輸入文件被編譯爲可執行文件,並會在控制檯上顯示全部編譯錯誤。dom

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;
}

如下文檔可供參考:ide

.NET中的動態編譯

動態源代碼生成和編譯(MSDN)

生成源代碼和在 CodeDOM 圖中編譯程序(MSDN)

一些重要的信息以下:post

使用 CodeDOM 代碼提供程序編譯程序集

調用編譯ui

若要使用 CodeDom 提供程序編譯程序集,必須有要用某種有編譯器的語言編譯的源代碼,或者有 CodeDOM 圖(可用來生成要編譯的源代碼)。url

若是從 CodeDOM 圖進行編譯,請將包含該圖的 CodeCompileUnit 傳遞給代碼提供程序的 CompileAssemblyFromDom 方法。若是您具備使用編譯器能夠理解的語言編寫的源代碼文件,請將包含源代碼的文件的名稱傳遞給 CodeDom 提供程序的 CompileAssemblyFromFile 方法。也能夠將包含用編譯器識別的語言編寫的源代碼的字符串傳遞給 CodeDom 提供程序的CompileAssemblyFromSource 方法。spa

配置編譯參數命令行

CodeDom 提供程序的全部標準編譯調用方法都有一個 CompilerParameters 類型的參數,該參數指示用於編譯的選項。debug

能夠在 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,配置編譯器在遇到警告時暫停編譯。

相關文章
相關標籤/搜索