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,配置编译器在遇到警告时暂停编译。

相关文章
相关标签/搜索