Using Roslyn’s Workspaces, is there any way to acquire a hook into the active IWorkspace provided by Visual Studio?
I want to be able to open ExampleProject in Visual Studio, then in my own application, I want to open ExampleProject again via Roslyn.Services.Workspace, but only for purposes of monitoring file/code changes that are produced in Visual Studio. I ran a hopeful test of just loading the project into a Workspace, and hoping it would magically pick up changes from disk somehow, but no such luck.
Which leaves the only possibilities being;
-
Get a reference to the current loaded Workspace directly from Visual
Studio. I don’t know if VS even exposes this across process
boundaries, or how to find out. -
Rework my entire project as a Visual Studio Extension, where I
would have automatic access to the open Workspace. I’d prefer not
to dive into building an Extension right now.
Here’s some code I’m using to test:
public class ProjectDiskMonitor
{
public ProjectDiskMonitor(string fullProjectPath)
{
WS = Workspace.LoadStandAloneProject(fullProjectPath, null, null, null, true) as Workspace;
WS.WorkspaceChanged += new EventHandler<WorkspaceEventArgs>(TargetProjectChanged);
//this event is not being fired as I edit the project via another instance of VS
}
private void TargetProjectChanged(object sender, WorkspaceEventArgs e)
{
Console.WriteLine("Project changed");
}
private Workspace WS
{
get;
set;
}
}
If you are calling Workspace.LoadSolution() to get the workspace, pass “true” to enableFileTracking parameter of LoadSolution and it’ll automatically listen to the filesystem and pick up changes. Don’t forget that ISolution instances are immutable, so to get new state you’ll have to access the CurrentSolution property on the Workspace to get the updated state.
There’s no current way to get an IWorkspace from Visual Studio in a cross-process manner, or at least not without you building your own remoting protocol.