Microsoft Roslyn GetDiagnostics()可以';t检测到错误

本文关键字:检测 错误 GetDiagnostics Roslyn 可以 Microsoft | 更新日期: 2023-09-27 18:29:47

我尝试使用Roslyn检查以下示例:

   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);

问题是GetDiagnostics()函数无法检测线路控制台上的错误。WriteLin*e*(….)

我做错了什么?

Microsoft Roslyn GetDiagnostics()可以';t检测到错误

这里的问题是SyntaxTree.GetDiagnostics()只会返回语法错误。换句话说,程序结构中的错误,而不是其含义中的错误。要获得预期的特定错误,您需要构造一个Compilation并获得编译的诊断,或者从SemanticModel获得SyntaxTree的诊断。

Kevin感谢您的回答。这是我的解决方案。
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);
        }
    }
}