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

丶灬走出姿态 提交于 2019-12-07 22:01:01

问题


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?


回答1:


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>();


来源:https://stackoverflow.com/questions/28198358/how-do-i-get-a-roslyn-workspace-from-a-vs-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!