问题
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.
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;
I tried following in unit test but workspace is null.
IWorkspace workspace = Workspace.PrimaryWorkspace;
ISolution solution = workspace.CurrentSolution;
I dont want to load solution, I want to work within currently loaded solution. How is it done? I am using Visual Studio 2012.
Edit:
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.
回答1:
As described by Dustin Campbell in his answer here: 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.
To enable the Roslyn languages services you need to start your VS with the following command:
devenv.exe /rootSuffix Roslyn
来源:https://stackoverflow.com/questions/19576043/roslyn-how-to-get-a-reference-to-workspace-from-currently-loaded-solution