4

From a Visual Studio 2015 CTP5 package, how do I get the current Roslyn workspace?

I looked at

How to get reference to 'Roslyn' Workspace object from IVsSolution?

and

Roslyn: How to get a reference to Workspace from currently loaded solution?

but I still can't make it work:

Workspace.CurrentWorkspace does not exist anymore

I have tried importing the VisualStudioWorkspace but it is still null:

public sealed class VSPackage1Package : Package
{
 ....
    [Import]
    public VisualStudioWorkspace Workspace { get; set; }
 ....   
    protected override void Initialize()
    {
    // Workspace is null here...

Is there a sample somewhere?

Community
  • 1
  • 1

1 Answers1

8

I don't see any definition of VisualStudioWorkspace (or the Microsoft.VisualStudio.LanguageServices assembly)

It's there, double check you're looking in the right place.

[Import]s only work if the class you're working in itself is MEF exported. If it's not (like a Package in your case), just write:

var componentModel = (IComponentModel)GetService(typeof(SComponentModel));
var workspace = componentModel.GetService<VisualStudioWorkspace>();
Jason Malinowski
  • 18,148
  • 1
  • 38
  • 55
  • You are right; the assembly is in the VSSDK folder. Thanks! – Paul Saint Martin Jan 28 '15 at 19:37
  • Jason, I have updated my question: Microsoft.VisualStudio.LanguageServices is here but it ignores the Import. – Paul Saint Martin Jan 29 '15 at 06:11
  • Updated. It's generally not recommended on StackOverflow to fundamentally change a question like that. Accept the answer and make a new question. Or, search more carefully, as this been asked a few times like [here](http://stackoverflow.com/questions/23578399/how-to-get-reference-to-roslyn-workspace-object-from-ivssolution) – Jason Malinowski Jan 29 '15 at 06:52
  • @PaulSaintMartin as of VS 2017, the assembly is no longer in the VS SDK folder. Rather, it resides at an obscure location under IDE\CommonExtensions, and I really doubt one is ever supposed to reference any assemblies from such folders that are apparently private to the IDE. Rather, I made it work by adding a NuGet package with the same name (just be careful with adding a specific version that corresponds to the version of your IDE - MSFT have documentation on that). – DmytroL May 27 '19 at 09:49
  • Yes, picking it up via NuGet is what we expect people to do. If it worked directly by picking it up directly from VS, that wasn't a build setup we really supported. – Jason Malinowski May 29 '19 at 05:16