Visual studio Visual Studio 2015扩展-如何获取当前解决方案的工作区

Visual studio Visual Studio 2015扩展-如何获取当前解决方案的工作区,visual-studio,Visual Studio,我正在开发Visual Studio 2015的扩展。如何将当前解决方案的workspace对象加载到IDE中,以便我的扩展可以处理它 许多示例似乎加载了一个项目或解决方案,如下所示,但是,我希望在IDE中加载解决方案的工作区,以便我的扩展可以访问它 Dim workspace = New AdhocWorkspace() Dim solution = workspace.CurrentSolution Dim project = solution.AddProject("projectN

我正在开发Visual Studio 2015的扩展。如何将当前解决方案的workspace对象加载到IDE中,以便我的扩展可以处理它

许多示例似乎加载了一个项目或解决方案,如下所示,但是,我希望在IDE中加载解决方案的工作区,以便我的扩展可以访问它

Dim workspace = New AdhocWorkspace()
Dim solution  = workspace.CurrentSolution
Dim project   = solution.AddProject("projectName", "assemblyName", LanguageNames.VisualBasic)
Dim document = project.AddDocument("name.vb", "...some code")

Roslyn定义了几种类型的工作空间,但您感兴趣的是VisualStudioWorkspace

您可以从vsix的构造函数通过MEF访问它:

[ImportingConstructor]
public Ctor([Import(typeof(SVsServiceProvider), AllowDefault = true)] IServiceProvider vsServiceProvider, [Import(typeof(VisualStudioWorkspace), AllowDefault = true)] Workspace vsWorkspace)
或者使用组件服务:

IComponentModel componentModel = this.serviceProvider.GetService(typeof(SComponentModel)) as IComponentModel;
var workspace = componentModel.GetService<Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace>();
IComponentModel componentModel=this.serviceProvider.GetService(typeof(scocomponentmodel))作为IComponentModel;
var workspace=componentModel.GetService();

您可能会发现,它也很有用。

将此解决方案所需的引用添加到VSIX项目中的最佳方法是什么?我需要哪些引用?如果您可以提供VB.NET版本的建议解决方案,将节省大量时间!我不熟悉VB.NET您必须为IComponentModel添加对Microsoft.VisualStudio.ComponentModelHost程序集的引用(通过添加引用->程序集->扩展),并为VisualStudioWorkspace添加对Microsoft.VisualStudio.LanguageServices程序集的引用(通过nuget)。为什么在第一种情况下使用IServiceProvider,而在第二种情况下使用组件模型?