[英]Roslyn - Can I subscribe to diagnostics being added to my compilation?
I am using the Roslyn SDK with StackExchange.Precompilation to create build-time auto-refactorings (precompiler macros) for C#. 我将Roslyn SDK与StackExchange.Precompilation结合使用,以为C#创建构建时自动重构(预编译器宏)。
During the process of syntax rewriting, it is very useful to add a Diagnostic
to the compilation when an error occurs or when a macro is used inappropriately in source code. 在语法重写过程中,当发生错误或在源代码中不适当地使用宏时,将
Diagnostic
添加到编译中非常有用。 Unlike throwing an exception, this displays errors in the VS Error List window and does not abort the thread like an exception might, so all build errors can be accumulated and listed for the user. 与引发异常不同,这会在“ VS错误列表”窗口中显示错误,并且不会像异常那样中止线程,因此可以为用户累积并列出所有构建错误。
However, for debugging the rewriter, it would be very handy to be able to subscribe to a DiagnosticAdded
event (or observable), so that a single breakpoint can be set to catch all diagnostics/errors. 但是,对于调试重写器,能够订阅
DiagnosticAdded
事件(或可观察的)非常方便,因此可以将单个断点设置为捕获所有诊断/错误。
Is there publisher in the Roslyn SDK (or StackExchange.Precompilation) like this? Roslyn SDK(或StackExchange.Precompilation)中是否有这样的发布者?
There's nothing public. 没有什么公开的。 Roslyn is open source, so you could build your own for testing and then set breakpoints in the diagnostic functions you're interested in.
Roslyn是开源的,因此您可以构建自己的测试软件,然后在感兴趣的诊断功能中设置断点。
I supect this question is related to StackExchange.Precompilation - How can I unit test precompilation diagnostics? 我想这个问题与StackExchange.Precompilation有关-如何对预编译诊断程序进行单元测试?
StackExchange.Precompilation doesn't have any events/publishers for this, but for testing purposes you could craft a After
/ BeforeCompileContext
with an IList<Diagnostics>
that allows observation, eg ObservableCollection
: StackExchange.Precompilation没有任何事件/发布程序,但是出于测试目的,您可以使用
IList<Diagnostics>
允许观察的结果来制作After
/ BeforeCompileContext
,例如ObservableCollection
:
CSharpCompilation compilation = ...;
ICompileModule testModule = ...
var diagnostics = new ObservableCollection<Diagnostics>();
var context = new BeforeCompileContext
{
Compilation = compilation,
Diagnostics = diagnostics,
};
diagnostics.CollectionChanged += (s, e) => { /* do something */ };
testModule.BeforeCompile(context);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.