Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a Roslyn workspace from a VS package?

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?

like image 411
Paul Saint Martin Avatar asked Feb 11 '23 01:02

Paul Saint Martin


1 Answers

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>();
like image 115
Jason Malinowski Avatar answered Mar 19 '23 04:03

Jason Malinowski