如何从 IVsSolution 获取对“Roslyn"工作区对象的引用? [英] How to get reference to 'Roslyn' Workspace object from IVsSolution?

查看:56
本文介绍了如何从 IVsSolution 获取对“Roslyn"工作区对象的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 VS Package 项目,我需要从中访问 Roslyn 或 Microsoft.CodeAnalysis 的工作区或解决方案对象从加载的 IVsSolution.

I have a VS Package project from which I need to access Roslyn or Microsoft.CodeAnalysis' Workspace OR Solution object from the loaded IVsSolution.

我需要知道如何才能做到这一点?

I need to know how I could achieve that ?

我发现了这个stackoverflow讨论这里 建议使用我在 Microsoft.CodeAnalysis.Workspace 中找不到的 Workspace 类的 PrimaryWorkspace 静态属性

I found this stackoverflow discussion here which suggests to use PrimaryWorkspace static property of Workspace class which I can't find in Microsoft.CodeAnalysis.Workspace

我发现 Microsoft.CodeAnalysis 还没有这个,但我下载了 来自 Nuget.org 的 Roslyn 的旧版本,它有这个.但是现在 PrimaryWorkspace 属性给了我 NULL :( 我使用的是隔离外壳.

I found out that Microsoft.CodeAnalysis does not have this yet but I downloaded the older release of Roslyn from Nuget.org which has this. But now PrimaryWorkspace Property is giving me NULL :( I am using Isolated Shell.

推荐答案

在 VSPackage 的 Initialize() 函数中,您可以使用以下内容:

Within the Initialize() function of your VSPackage, you can use the following:

var componentModel = (IComponentModel)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SComponentModel));
var workspace = componentModel.GetService<Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace>();

我相信您还需要添加对以下内容的额外引用:Microsoft.VisualStudio.LanguageServices.dll

I believe you'll also need to add an additional reference to: Microsoft.VisualStudio.LanguageServices.dll

正如@Vizu 所指出的,您现在可以通过 NuGet 添加:

As noted by @Vizu, you can now add this via NuGet:

Install-Package Microsoft.VisualStudio.LanguageServices

这篇关于如何从 IVsSolution 获取对“Roslyn"工作区对象的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆