Roslyn:如何从当前加载的解决方案中获取对 Workspace 的引用? [英] Roslyn: How to get a reference to Workspace from currently loaded solution?

查看:41
本文介绍了Roslyn:如何从当前加载的解决方案中获取对 Workspace 的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取对当前加载的工作区的引用,但没有成功.根据文档(粗体部分),我应该能够获得对它的引用.

I am trying to get a reference to currently loaded workspace, without success. As per documentation (part in bold) I should be able to get a reference to it.

工作区 API 位于 Roslyn.Services 命名空间中,并且如果您包含以下 using 指令,则它们可用:

The Workspace APIs are found in the Roslyn.Services namespace, and they are available if you include the following using directive:

使用 Roslyn.Services;

using Roslyn.Services;

您使用的工作区通常由主持人直接提供环境(例如 Visual Studio IDE).但是,您可以工作通过构建您的主机环境之外的工作区自己的 IWorkspace 实例.

The workspace you use will typically be provided directly by the host environment (such as the Visual Studio IDE). However, you can work with a workspace outside of a host environment by constructing your own IWorkspace instance.

您可以通过加载解决方案文件来构建工作区.

You can construct a workspace by loading a solution file.

IWorkspace workspace = Workspace.LoadSolution(@"HelloWorld.sln");ISolution 解决方案 = workspace.CurrentSolution;

IWorkspace workspace = Workspace.LoadSolution(@"HelloWorld.sln"); ISolution solution = workspace.CurrentSolution;

我在单元测试中尝试了以下操作,但工作区为空.

I tried following in unit test but workspace is null.

  IWorkspace workspace = Workspace.PrimaryWorkspace;

  ISolution solution = workspace.CurrentSolution;

我不想加载解决方案,我想在当前加载的解决方案中工作.它是如何完成的?我使用的是 Visual Studio 2012.

I dont want to load solution, I want to work within currently loaded solution. How is it done? I am using Visual Studio 2012.

尝试按照回答中的建议使用开关/rootSuffx Roslyn,VS 抛出一个错误,指出它是无效开关.改成/rootSuffix Roslyn,VS启动了但是workspace还是null.

Tried using switch /rootSuffx Roslyn as suggested in answer and VS throws an error that it is invalid switch. Changed it to /rootSuffix Roslyn, and VS starts but workspace is still null.

推荐答案

正如 Dustin Campbell 在他的回答中所描述的:如何使用VSPackage内的Workspace.PrimaryWorkspace.CurrentSolution

As described by Dustin Campbell in his answer here: How to work with Workspace.PrimaryWorkspace.CurrentSolution inside VSPackage

仅在启用 Roslyn C# 和 Visual Basic 语言服务时才会填充 Visual Studio 内的主要工作区.

The primary workspace inside of Visual Studio is only populated when the Roslyn C# and Visual Basic language services are enabled.

要启用 Roslyn 语言服务,您需要使用以下命令启动 VS:

To enable the Roslyn languages services you need to start your VS with the following command:

devenv.exe /rootSuffix Roslyn

这篇关于Roslyn:如何从当前加载的解决方案中获取对 Workspace 的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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