【问题标题】:Visual Studio 2015 Extension - How to get workspace of current SolutionVisual Studio 2015 Extension - 如何获取当前解决方案的工作区
【发布时间】:2015-04-22 20:09:53
【问题描述】:

我正在开发 Visual Studio 2015 的扩展。如何获取加载到 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")

【问题讨论】:

    标签: visual-studio


    【解决方案1】:

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

    您可能会发现this questionthis question 也很有用。

    【讨论】:

    • 将此解决方案所需的引用添加到 VSIX 项目的最佳方法是什么?我需要哪些引用?如果你能提供VB.NET版本的建议解决方案,那将节省大量时间!
    • 我对VB.NET不熟悉
    • 您必须为 IComponentModel 添加对 Microsoft.VisualStudio.ComponentModelHost 程序集的引用(通过添加引用 -> 程序集 -> 扩展),并为 VisualStudioWorkspace 添加对 Microsoft.VisualStudio.LanguageServices 程序集的引用(通过 nuget)。
    • 为什么在第一种情况下你使用IServiceProvider ,但在第二种情况下你使用SComponentModel
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2015-10-10
    相关资源
    最近更新 更多