首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Microsoft.CodeAnalysis.Diagnostic获取错误行

如何使用Microsoft.CodeAnalysis.Diagnostic获取错误行
EN

Stack Overflow用户
提问于 2021-06-03 16:49:31
回答 1查看 97关注 0票数 1

我希望你有美好的一天。我有一段代码用于在运行时评估代码:

代码语言:javascript
复制
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText("Code goes here...");
string assemblyName = Path.GetRandomFileName();

            string corePath = Path.GetDirectoryName(typeof(object).GetTypeInfo().Assembly.Location);
            MetadataReference[] references = new MetadataReference[]
            {
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location),
                MetadataReference.CreateFromFile("Newtonsoft.Json.dll"),
                MetadataReference.CreateFromFile(Path.Combine(corePath, "System.Net.dll")),
                MetadataReference.CreateFromFile(Path.Combine(corePath, "System.Text.RegularExpressions.dll"))
            };

            CSharpCompilation compilation = CSharpCompilation.Create(
                assemblyName,
                syntaxTrees: new[] { syntaxTree },
                references: references,
                options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            using (var ms = new MemoryStream())
            {
                EmitResult result = compilation.Emit(ms);

                if (!result.Success)
                {
                    IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
                        diagnostic.IsWarningAsError ||
                        diagnostic.Severity == DiagnosticSeverity.Error);

                    string errors = "";

                    foreach (Diagnostic diagnostic in failures)
                    {
                        errors += "<span style=\"text-align: center;\">Error <b>" + diagnostic.Id + "</b>: " + diagnostic.GetMessage() + "</span><br>";
                    }

                    return errors;
                }
                else
                {
                    ms.Seek(0, SeekOrigin.Begin);
                    Assembly assembly = Assembly.Load(ms.ToArray());

                    Type type = assembly.GetType("NoteBoxCode.Program");
                    object obj = Activator.CreateInstance(type);
                    return type.InvokeMember("Main", BindingFlags.InvokeMethod, null, obj, null);
                }
            }

例如,当我得到一个错误时,它只会显示这个:Error CS1520: Method must have a return type

如何获得像Error on line 2 CS1520: Method must have a return type这样的输出

我尝试过diagnostic.Location,但我认为它返回的是装配线位置,这是我不需要的。有什么帮助吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-07 14:47:07

因此,CSharpCompilationOptions不能返回错误行,因为它是运行时错误。我需要一个高级调试类来分别调试和运行每一行代码,这就是(我认为) visual studio所做的。

您可以使用SyntaxTree.GetDiagnostics()来获取语法错误的概述。但是,这不会返回编译错误,例如Console.WritLine而不是Console.WriteLine

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67818314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档