访问编译器的错误和警告

555*_*555 4 c#

我正在为我的一个大学课程开发一个简单的C#编辑器,我需要将一个.cs文件发送给编译器并收集错误(如果它们存在)并在我的应用程序中显示它们.换句话说,我想将C#编译器添加到我的编辑器中.调试器有这样的东西吗?

Shu*_*oUk 8

如果您使用CodeDomProvider

using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

// the compilation part

// change the parameters as you see fit
CompilerParameters cp = CreateCompilerParameters(); 
var options = new System.Collections.Generic.Dictionary<string, string>();
if (/* you want to use the 3.5 compiler*/)
{
    options.Add("CompilerVersion", "v3.5");
}
var compiler = new CSharpCodeProvider(options);
CompilerResults cr = compiler.CompileAssemblyFromFile(cp,filename);
if (cr.Errors.HasErrors)
{
    foreach (CompilerError err in cr.Errors)
    {
        // do something with the error/warning 
    }
}
Run Code Online (Sandbox Code Playgroud)