• Microsoft.CSharp.CSharpCodeProvider


    Microsoft.CSharp.CSharpCodeProvider

    MSDN

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

  • 相关阅读:
    SpringBoot和SpringCould的关系
    MyBatis全局配置文件头
    MyBatis的SQL映射文件头
    MyBatis 驼峰式配置 yml配置
    频率组件
    序列化和反序列化
    生成器面试题
    序列化组件
    进程间通信IPC机制
    信号量、event事件和线程queue
  • 原文地址:https://www.cnblogs.com/bruceleeliya/p/4118798.html
Copyright © 2020-2023  润新知