码迷,mamicode.com
首页 > 其他好文 > 详细

Microsoft.CSharp.CSharpCodeProvider

时间:2014-11-24 17:03:07      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

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)

Microsoft.CSharp.CSharpCodeProvider

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://www.cnblogs.com/bruceleeliya/p/4118798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!