Roslyn 分析器运行不够频繁
我正在编写我的第一个 Roslyn 分析器。我基本上遵循了教程https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/special-issue/csharp-and-visual-basic-use-roslyn-to-write-a-live-code -analyzer-for-your-api ,然后继续添加第二个分析器类,该类应该是与语言无关的分析,类似于 https://www.meziantou.net/writing- a-语言无关-roslyn-analyzer-using-ioperation.htm 。
因此,我有一个分析器类,它初始化自身
context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType);
,第二个分析器类使用它
context.RegisterOperationAction(AnalyzeConversionOperation, OperationKind.Conversion);
context.RegisterOperationAction(AnalyzeInvocationOperation, OperationKind.Invocation);
来测试我的分析器,我将生成的 .Vsix 项目设置为启动项目,并通过按 F5,它使我进入 Visual Studio 的一个单独实例,其中我编写了一些代码,想看看我的分析器是否按预期工作。而且,我在原始 VS 实例中的分析器操作(AnalyzeSymbol、AnalyzeConversionOperation 和AnalyzeIncationOperation)开始时设置了断点。
分析器操作也没有像我期望的那样被调用。事实上,当我编写要分析的代码时,它们根本没有被调用。仅当我将光标放在某个类型名称上(与 SymbolKind.NamedType 操作相关)时,并且我单击出现的灯泡,我才会收到调用 - 而不仅仅是对 AnalyzeSymbol 操作,而且还会根据代码中的各种操作的需要调用AnalyzeConversionOperation 和AnalyzeInitationOperation。
因此,被调用的操作都很好 - 但当我想要它们时它们没有被调用。我希望它们能够根据需要被调用——基本上,在我编辑代码时几乎是连续的。或者至少在构建时,或者一些明确的“立即分析”命令。但我不知道有什么类似的事情。我发现触发它们的唯一方法就是我所描述的方法。这对我来说似乎不正确。
我尝试谷歌搜索但找不到解决方案;或者可能我有一些误解,它应该以不同的方式工作?
I am writing my first Roslyn analyzers. I have basically followed the tutorial https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/special-issue/csharp-and-visual-basic-use-roslyn-to-write-a-live-code-analyzer-for-your-api , and then proceeded by adding a second analyzer class which should be language-agnostics analysis, similar to what presented in https://www.meziantou.net/writing-a-language-agnostic-roslyn-analyzer-using-ioperation.htm .
So, I have one analyzer class which initializes itself with
context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType);
and a second one which uses
context.RegisterOperationAction(AnalyzeConversionOperation, OperationKind.Conversion);
context.RegisterOperationAction(AnalyzeInvocationOperation, OperationKind.Invocation);
To test my analyzers, I have the generated .Vsix project set as startup project, and by pressing F5, it gets me to a separate instance of Visual Studio where I write some code and want to see whether my analyzers work as intended. And, I set breakpoints at the begininng of my analyzer actions - AnalyzeSymbol, AnalyzeConversionOperation and AnalyzeInvocationOperation, in the original VS instance.
The analyzer actions are not called as I would expect them too. In fact, they are not being called at all, as I write the code to be analyzed. Only if I place the cursor on some type name (which is related to the SymbolKind.NamedType action), and I click on the lightbulb that appears, I get the calls - and not only to the AnalyzeSymbol action, but also the AnalyzeConversionOperation and AnalyzeInvocationOperation are called as needed for various operations in the code.
So, the actions that get called are all fine - but they not called when I want them. I would expect them to be called as needed - basically, almost continuously as I edit the code. Or at least upon build, or some explicit "Analyze Now" command. But I am not aware of anything alike. The only way I found to trigger them is the way I described. That does not seem correct to me.
I tried Googling but could not find the solution; or possibly I have some misconception and it is supposed to work differently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
分析器在进程外运行。因此,我认为您不会遇到断点,但实际上这些行会被命中。
您有三个选项:
禁用进程外运行分析器。
附加
ServiceHub.RoslynCodeAnalysisService< /code> 处理调试器。
通过单元测试进行调试。这是我首选的方法。
Analyzers are run out of process. So I think it's expected that you don't get the breakpoints hit, but in fact these lines get hit.
You have three options:
Disable running analyzers out-of-process.
Attach
ServiceHub.RoslynCodeAnalysisService
process to the debugger.Debug through a unit test. This is my preferred approach.