Microsoft Roslyn GetDiagnostics () cannot perform error checking

I tried using Roslyn to check the following example:

static void Main(string[] args) { string sScriptCsx = @" using System; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLin(""Hello, World!""); } } }"; string strDetail = ""; Diagnostic obj; SyntaxTree stTree = SyntaxTree.ParseText(sScriptCsx); if (stTree.GetDiagnostics().Count == 0) { strDetail += "La génération a réussi. Aucune erreur détectée."; Environment.Exit(0); } for (int i = 0; i < stTree.GetDiagnostics().Count; i++) { obj = stTree.GetDiagnostics()[i]; strDetail += "<b>" + (i + 1).ToString() + ". Info: </b>" + obj.Info.ToString() + Environment.NewLine; strDetail += " <b>Warning Level: </b>" + obj.Info.WarningLevel.ToString() + Environment.NewLine; strDetail += " <b>Severity Level: </b>" + obj.Info.Severity.ToString() + Environment.NewLine; strDetail += " <b>Location: </b>" + obj.Location.Kind.ToString() + Environment.NewLine; strDetail += " <b>Character at: </b>" + obj.Location.GetLineSpan(true).StartLinePosition.Character.ToString() + Environment.NewLine; strDetail += " <b>On Line: </b>" + obj.Location.GetLineSpan(true).StartLinePosition.Line.ToString() + Environment.NewLine; strDetail += Environment.NewLine; } Console.WriteLine(strDetail); 

The problem is that the GetDiagnostics () function cannot cut the error in Line Console.WriteLin * e * (....)

What am I doing wrong?

+4
source share
2 answers

The problem is that SyntaxTree.GetDiagnostics() will only return syntax errors. In other words, errors in the structure of the program, not errors in its meaning. To get the specific error you expect, you will need to build Compilation and get the diagnostics to compile or from the SemanticModel for your SyntaxTree .

+7
source

Kevin thanks you for the answer. Here is my solution.

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Roslyn.Compilers; using Roslyn.Compilers.CSharp; using Roslyn.Compilers.Common; using Roslyn.Scripting; using Roslyn.Scripting.CSharp; // // To install Roslyn, run the following command in the Package Manager Console : PM> Install-Package Roslyn // namespace WebScriptChecker { class Program { static void Error(params string[] errors) { var oldColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; foreach (var o in errors) { Console.Write(o.ToString()); } Console.Write(Environment.NewLine); Console.ForegroundColor = oldColor; } public static void Execute(string code) { CommonScriptEngine engine = new ScriptEngine(); Session session = engine.CreateSession(); try { Submission<object> submission = session.CompileSubmission<object>(code); object result = submission.Execute(); bool hasValue; ITypeSymbol resultType = submission.Compilation.GetSubmissionResultType(out hasValue); } catch (CompilationErrorException e) { Error(e.Diagnostics.Select(d => d.ToString()).ToArray()); } catch (Exception e) { Error(e.ToString()); } } static void Main(string[] args) { string sScriptCsx = @" using System; class Program { static void Main(string[] args) { Console.WriteLine(""Hello, World!""); } } "; Execute(sScriptCsx); Console.WriteLine(); Console.Write("Presser une touche pour continuer ... "); Console.ReadKey(); Environment.Exit(1); } } } 
+1
source

Source: https://habr.com/ru/post/1500078/


All Articles