Roslyn:如何从当前加载的解决方案中获取对工作区的引用?

标签 roslyn

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

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

using Roslyn.Services;

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 solution = workspace.CurrentSolution;

我尝试在单元测试中执行以下操作,但工作区为空。

  IWorkspace workspace = Workspace.PrimaryWorkspace;

  ISolution solution = workspace.CurrentSolution;

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

编辑:

按照答案中的建议尝试使用开关/rootSuffx Roslyn ,VS 抛出一个错误,表明它是无效开关。将其更改为/rootSuffix Roslyn,VS 启动但工作区仍然为空。

最佳答案

正如达斯汀坎贝尔在他的回答中所描述的:How to work with Workspace.PrimaryWorkspace.CurrentSolution inside VSPackage

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

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

devenv.exe /rootSuffix Roslyn

关于Roslyn:如何从当前加载的解决方案中获取对工作区的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19576043/

相关文章:

c# - 从 CatchDeclaration 获取符号

roslyn - 在 Roslyn API 中,推测语义模型是什么?

c# - 如何限制从 Roslyn 脚本引擎访问程序集中的成员?

c# - 通过 SyntaxFactory (Roslyn) 构造 NameOf 表达式

c# - 使用 Roslyn 引用 PCL 库会导致 .NET 版本问题

.net - 使用Roslyn进行新的语言实现

reference - Roslyn CSharpScript 和 DateTime 引用错误

Roslyn 分析器对象初始值设定项

c# - Roslyn CTP - 如何将更改写入文件系统

c# - 我如何通过roslyn找到变量的值