如何使用 Microsoft.CodeAnalysis.Diagnostic 获取错误行
How to get error line using Microsoft.CodeAnalysis.Diagnostic
希望你今天过得愉快。
我有这段代码用于在运行时评估代码:
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
,但我认为 returns 流水线位置,我不需要。有帮助吗?
因此,CSharpCompilationOptions
不能 return 错误行,因为它是 运行 时间错误。我将需要高级调试 class,单独调试和 运行 每行代码,这就是(我认为)visual studio 所做的。
您可以使用 SyntaxTree.GetDiagnostics()
来大致了解语法错误。但是这不会 return 编译错误,例如 Console.WritLine
而不是 Console.WriteLine
.
希望你今天过得愉快。 我有这段代码用于在运行时评估代码:
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
,但我认为 returns 流水线位置,我不需要。有帮助吗?
因此,CSharpCompilationOptions
不能 return 错误行,因为它是 运行 时间错误。我将需要高级调试 class,单独调试和 运行 每行代码,这就是(我认为)visual studio 所做的。
您可以使用 SyntaxTree.GetDiagnostics()
来大致了解语法错误。但是这不会 return 编译错误,例如 Console.WritLine
而不是 Console.WriteLine
.