简体   繁体   English

从Visual Studio扩展中的ProjectItem获取Roslyn SemanticModel

[英]Getting Roslyn SemanticModel from ProjectItem in Visual Studio extension

I'm writing a Visual Studio 2015 extension which looks at the contents of the class the user has right clicked on. 我正在编写一个Visual Studio 2015扩展,它查看用户右键单击的类的内容。

I've got the ProjectItem , but how do you get the SemanticModel (and SyntaxTree ) from this? 我有ProjectItem ,但是如何从中获取SemanticModel (和SyntaxTree )?

I need to look up some types of properties declared in the file. 我需要查找文件中声明的某些类型的属性。 I've written a code analyzer which gives you the SemanticModel on the context but I can't figure out how to get it here. 我编写了一个代码分析器,它为您提供了上下文中的SemanticModel ,但我无法弄清楚如何在此处获取它。 Searches haven't turned up anything useful. 搜索没有发现任何有用的东西。 I've found how to parse the SyntaxTree by reading the file contents, but it won't be so easy with the SemanticModel . 我已经找到了如何通过读取文件内容来解析SyntaxTree ,但是使用SemanticModel并不是那么容易。 Ideally I'd hook in to the model VS has already built for the file. 理想情况下,我会挂钩VS已经为该文件构建的模型。

Figured it out. 弄清楚了。

  1. Upgrade your project to target .NET framework 4.6.2. 将项目升级到目标.NET Framework 4.6.2。
  2. Install the Microsoft.VisualStudio.LanguageServices NuGet package . 安装Microsoft.VisualStudio.LanguageServices NuGet包
  3. Downgrade System.Collections.Immutable from 1.2.0 to 1.1.37, otherwise you get a MissingMethodException in the code later. 将System.Collections.Immutable从1.2.0降级到1.1.37,否则稍后会在代码中收到MissingMethodException。
  4. In the package class's Initialize method, grab the VisualStudioWorkspace. 在包类的Initialize方法中,获取VisualStudioWorkspace。 I save it in a static property here to grab later: 我将它保存在静态属性中以便稍后获取:

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

Now you can get the Document with SyntaxTree and SemanticModel from the file path: 现在,您可以从文件路径获取带有SyntaxTree和SemanticModel的Document:

Microsoft.CodeAnalysis.Solution solution = CreateUnitTestBoilerplateCommandPackage.VisualStudioWorkspace.CurrentSolution;
DocumentId documentId = solution.GetDocumentIdsWithFilePath(inputFilePath).FirstOrDefault();
var document = solution.GetDocument(documentId);

SyntaxNode root = await document.GetSyntaxRootAsync();
SemanticModel semanticModel = await document.GetSemanticModelAsync();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Visual Studio扩展中找出ProjectItem的源控件状态 - Find out the source control state of a ProjectItem from a Visual Studio extension Visual Studio扩展:从文件名或ProjectItem获取语言/文件类型 - Visual Studio extension: get language / file type from file name or ProjectItem 更改Visual Studio 2015扩展中项目中文件ProjectItem的内容吗? - Change the contents of a file ProjectItem within a Project in a Visual Studio 2015 Extension? 扩展Visual Studio根据文件路径获取ProjectItem或IVsHierarchy - Extending Visual Studio getting ProjectItem or IVsHierarchy based on file path 有没有办法在Roslyn中可视化SemanticModel - Is there a way to visualize the SemanticModel in Roslyn 如何在当前Visual Studio主机内的Visual Studio扩展中调试使用Roslyn编译的代码? - How to debug code compiled with Roslyn in a Visual Studio extension inside current Visual Studio host? 如何使用Roslyn SemanticModel通过ScriptCompilation获取TypeInfo - How to use Roslyn SemanticModel to get TypeInfo with ScriptCompilation 在Visual Studio中使用自编译的Roslyn? - Using self-compiled Roslyn from within Visual Studio? 在 Visual Studio 中获取文件的内容而无需从扩展中打开文件 - Getting the contents of a file in Visual Studio without opening the file from an extension Roslyn诊断+ Visual Studio着色 - Roslyn Diagnostic + Visual Studio Colorisation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM