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