Roslyn:带有代码修复项目的Diagnostic中的当前工作区 [英] Roslyn: Current Workspace in Diagnostic with code fix project

查看:46
本文介绍了Roslyn:带有代码修复项目的Diagnostic中的当前工作区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在带有代码修复项目的诊断程序中获取有关当前工作区的信息(例如,项目路径,解决方案路径)?

How can I get information about current Workspace (e.g project path, solution path) in Diagnostic with code fix project?

我正在实现ISyntaxNodeAnalyzer类型的诊断程序

I am implementing Diagnostic of type ISyntaxNodeAnalyzer

我需要访问SymbolFinder.FindImplementationsAsync,但为此,我需要解决方案实例

I need to access SymbolFinder.FindImplementationsAsync, but to do so, I need Solution instance

我有这样的代码:

var syntax = (LocalDeclarationStatementSyntax) node;
var type = syntax.Declaration.Type;
var typeSymbol = semanticModel.GetTypeInfo(type).ConvertedType;

我想找出typeSymbol的所有用法/引用.TypeSymbol表示位于源代码中的Class.

I would like to find out all usages / references of typeSymbol. TypeSymbol represents Class located in source code.

为此,我想使用SymbolFinder,但是SymbolFinder的方法需要Solution的实例...在旧版本的Roslyn中,文档被指定为诊断的方法参数",您可以进入项目和解决方案.

To do so, I wanted to use SymbolFinder, but methods of SymbolFinder require instance of Solution... In older version of Roslyn, Document was given as Method Parameter of diagnostics, you could get to project and solution.

推荐答案

在实现诊断分析器时,目前尚无进入工作区或进行多项目分析的方法,因为这些方法必须能够针对单个运行仅使用命令行编译器进行编译.但是,一旦确定诊断,您就可以在代码修复提供程序中完成这项工作.

There is no current way to get at workspace or to do multi-project analysis when implementing a diagnostic analyzer, as these need to be able to run against a single compilation using only the command-line compiler. You can, however, do this work in the code fix provider once a diagnostic is determined.

如果只想查找一旦某个类实现了另一个位置的所有位置,则可以制作一个编译范围的诊断分析器,并在决定诊断之前检查所有声明.您可以同时实现ICompilationStartedAnalyzer和ICompilationEndedAnalyzer.

If you just want to find all places where once class implements another, you can make a compilation wide diagnostic analyzer and examine all declarations before deciding on a diagnostic. You can do this by implementing both ICompilationStartedAnalyzer and ICompilationEndedAnalyzer.

这篇关于Roslyn:带有代码修复项目的Diagnostic中的当前工作区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆