如何在.NET代码分析器中获取解决方案路径 [英] How to get solution path in .NET code analyzer

查看:100
本文介绍了如何在.NET代码分析器中获取解决方案路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何访问Roslyn代码分析器中正在编译的项目/解决方案的文件路径?我需要对照一些相对于代码存储的规范文件来验证代码。无效的东西:

How do you get access the file path to the project/solution being compiled inside a Roslyn code analyzer? I need to verify the code against some spec files stored relative to the code. Things that do NOT work:

SyntaxTreeAnalysisContext.Tree.FilePath
Assembly.GetExecutingAssembly().Location
AppDomain.CurrentDomain.BaseDirectory
Environment.CurrentDirectory
Path.GetFullPath(relativePath)


推荐答案

分析器位于工作区级别以下(它们由编译器直接运行),因此该解决方案可能不存在。

Analyzers exist below the Workspace level (they're run directly by the compiler), so the solution may not exist.

由于复杂的原因,它们不是由MEF创建的,因此即使存在,也没有简单的方法来获得它。

For complicated reasons, they aren't created by MEF, so there is no easy way to get to it even if it does exist.

在VS中,您可以找到全局服务提供者(例如, ServiceProvider.GlobalProvider ),然后获取 SComponentModel (VS自己的MEF图的根),然后从中获取Roslyn的 VisualStudioWorkspace 。请注意,这是一种比较脆弱的方法,并且在VS之外根本无法使用。

From within VS, you can find the global service provider (eg, ServiceProvider.GlobalProvider), then get SComponentModel (the root of VS's own MEF graph) and grab Roslyn's VisualStudioWorkspace from that. Beware that this is a somewhat brittle approach, and will not work at all outside VS.

即使在VS内,这也会以奇怪的方式破坏预览窗格中的分析,其他文件和其他不属于全局解决方案的上下文。

Even within VS, this will break in strange ways for analyses in Preview panes, Miscellaneous Files, and other contexts that are not part of the global solution.

这篇关于如何在.NET代码分析器中获取解决方案路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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