Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to Compiler's errors and warnings

Tags:

c#

i'm developing a simple C# Editor for one of my university courses and I need to send a .cs file to Compiler and collect errors (if they exist) and show them in my app. In other words i want to add a C# Compiler to my Editor. Is there anything like this for the debugger?

like image 747
55555 Avatar asked Jan 24 '23 21:01

55555


2 Answers

If you use the 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 
    }
}
like image 132
ShuggyCoUk Avatar answered Jan 26 '23 11:01

ShuggyCoUk


Use the System.Diagnostics.Process class to start a generic process and collect the I/O from standard input/output.

For this specific scenario, I suggest you look at Microsoft.CSharp.CSharpCodeProvider class. It'll do the task for you.

like image 27
mmx Avatar answered Jan 26 '23 12:01

mmx