從 Visual Studio 擴充套件中獲取 VisualStudioWorkspace

與其他型別的工作空間相比,VisualStudioWorkspace 無法手動建立。構建 Visual Studio 擴充套件時可以訪問它。

在擴充套件包專案中,轉到 [YourVSPackage]Package.cs 檔案。在那裡,你可以通過兩種方式獲取工作區:

protected override void Initialize()
{ 
    // Additional code...

    var componentModel = (IComponentModel)this.GetService(typeof(SComponentModel));
    var workspace = componentModel.GetService<VisualStudioWorkspace>();
}

或者使用 MEF:

[Import(typeof(VisualStudioWorkspace))]
public VisualStudioWorkspace ImportedWorkspace { get; set; }

關於 VisualStudioWorkspace 的精彩視訊教程可以在這裡找到。