简体   繁体   English

如何在DiagnosticAnalyzer和CodeFixProvider中进入工作区? (罗斯林)

[英]How to get to Workspace in DiagnosticAnalyzer and CodeFixProvider? (Roslyn)

I want to check whether the configuration of a Method (Like Logger) is added to appsettings.json . 我想检查是否将方法(如Logger)的配置添加到appsettings.json If not, then I want to add it through Code Fix. 如果没有,那么我想通过代码修复添加它。

I am trying to access the Workspace so that I can access the documents in it. 我正在尝试访问工作区,以便可以访问其中的文档。 But I can't find a way to get the current workspace. 但是我找不到找到当前工作空间的方法。

I have tried to use AdhocWorkspace but the projects list is empty in it 我尝试使用AdhocWorkspace但是项目列表为空

var solution = new AdhocWorkspace().CurrentSolution;

In a CodeFixProvider , you can access the workspace from the CodeFixContext that is provided to the RegisterCodeFixesAsync method: CodeFixProvider ,可以从提供给RegisterCodeFixesAsync方法的CodeFixContext中访问工作区:

public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
    var workspace = context.Document.Project.Solution.Workspace;
    //...
}

For a DiagnosticAnalyzer , it's a different story. 对于DiagnosticAnalyzer ,情况就不同了。 The analyzers work with compilations, not with particular projects or documents. 分析器只处理汇编,而不处理特定项目或文档。 There is no way to reach the workspace when implementing a diagnostic analyzer, because the analyzers need to be able to run against a single compilation using only the command-line compiler. 实施诊断分析器时无法到达工作空间,因为分析器需要能够仅使用命令行编译器针对单个编译运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM