简体   繁体   English

Roslyn-我可以订阅被添加到我的汇编中的诊断程序吗?

[英]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.

相关问题 我如何对Roslyn诊断进行单元测试? - How can I unit test Roslyn diagnostics? 我可以为我的扩展程序获得Roslyn分析器,但无法获得用于同一诊断的codeFix提供程序 - I am able to get the Roslyn analyzer for my extension but failed to get the codeFix provider for the same diagnostics 如何使用Roslyn代码诊断识别和分析局部变量和参数? - How can I identify and analyze local variables and parameters with Roslyn code diagnostics? 罗斯林诊断程序注册 - Roslyn diagnostics registration 如何使用自己的密钥对签署 Roslyn - How can I use my own key pair for signing Roslyn Roslyn MsBuildWorkspace编译发出的内容不包含我的更改 - Roslyn MsBuildWorkspace compilation emit does not contain my changes 无法创建新的“代码修复诊断”项目! (罗斯林+VS2013) - Can't create new "Diagnostics with code fix" project! (Roslyn+VS2013) Roslyn:如何判断编译中是否引用了特定的程序集? - Roslyn: How do I tell if a particular assembly is referenced in a Compilation? 带有 Roslyn 编译的嵌入式文件 - Embedded files with Roslyn compilation 如果我支持VS2015,我的分析器可以定位的Roslyn的最新版本是什么? - What's the latest version of Roslyn my analyzer can target if I support VS2015?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM