简体   繁体   English

将Roslyn MSBuildWorkspace与Visual Studio 2013一起使用

[英]Use Roslyn MSBuildWorkspace with Visual Studio 2013

For my master thesis I'm building a Visual Studio plugin that should perform some code-analysis of the current opened solution. 对于我的硕士论文,我正在构建一个Visual Studio插件,该插件应该对当前打开的解决方案执行一些代码分析。 In order to do that I've decided to try to use Roslyn, using the corresponding nuget package . 为了做到这一点,我决定尝试使用Roslyn,使用相应的nuget包

Everything works fine (SyntaxTree for navigating the code,...) until I've tried to use MSBuildWorkspace.Create(). 一切正常(SyntaxTree用于导航代码,......),直到我尝试使用MSBuildWorkspace.Create(). This last call causes the following exception: 最后一次调用会导致以下异常:

Could not load file or assembly 'Microsoft.Build, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. 无法加载文件或程序集'Microsoft.Build,Version = 14.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'或其依赖项之一。 The system cannot find the file specified.":"Microsoft.Build, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 系统找不到指定的文件。“:”Microsoft.Build,Version = 14.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a

I found this two posts: 我找到了这两个帖子:

from which I understand that I need the MSBuild Tools for Visual Studio 14, which is in the related iso. 据我所知,我需要Visual Studio 14的MSBuild工具,它位于相关的iso中。

I do not want to install the full Visual Studio 14, this also because the plugin that I'm writing should run under Visual Studio 2013. From the two post it seems that I can install only this part of VS 14. 我不想安装完整的Visual Studio 14,这也是因为我正在编写的插件应该在Visual Studio 2013下运行。从这两个帖子看来我似乎只能安装VS 14的这一部分。

My question actually is: if I install MSBuild Tools for Visual Studio 14 what will happen with all other Visual Studio projects that I'm currently working on? 我的问题实际上是:如果我为Visual Studio 14安装MSBuild工具,那么我目前正在处理的所有其他Visual Studio项目会发生什么? At the moment they use the MSBuild tool of Visual Studio 2013. It's possible to still use that? 目前他们使用Visual Studio 2013的MSBuild工具。仍然可以使用它?

Update 更新

What I'm acctually trying to get is to find if a given method has references inside the project. 我实际上想要得到的是找到一个给定的方法是否在项目中有引用。 The idea was to proceed as in this post . 这个想法是按照这篇文章进行的

When you say you're building a plugin for Visual Studio, if all you care about is getting the workspace for the currently open solution (ie the code the user is editing), you shouldn't use MSBuildWorkspace, really ever. 当您说您正在为Visual Studio构建插件时,如果您关心的是获取当前打开的解决方案的工作区(即用户正在编辑的代码),那么您不应该使用MSBuildWorkspace。 That's a type for loading things outside of Visual Studio. 这是一种 Visual Studio 之外加载东西的类型。 What you can do if you're in the product is MEF [Import] Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace. 如果您在产品中,可以执行的操作是MEF [导入] Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace。 That gives you live access to what the user has open, and avoids running MSBuild entirely. 这使您可以实时访问用户打开的内容,并避免完全运行MSBuild。

Note you're still going to have to be careful about your choice of reference assemblies you build against: if you use the latest NuGet packages that won't work since those will differ in version (and also APIs -- we changed a bunch of stuff) than what was in the last Visual Studio 2013 preview. 请注意,您仍然需要谨慎选择您构建的引用程序集:如果您使用的是最新的NuGet程序包,因为它们的版本不同(以及API)我们更改了一堆东西)比上一个Visual Studio 2013预览中的东西。

The issue is that (unfortunately) the assemblies in the public Roslyn nuget packages have been compiled with a newer version of MSBuild than what you want. 问题是(不幸的是)公共Roslyn nuget包中的程序集已使用比您想要的更新版本的MSBuild进行编译。

However, it's pretty simple to fix so that it works on MSBuild 4.0 (VS2012+). 但是,修复非常简单,以便它可以在MSBuild 4.0(VS2012 +)上运行。 I've provided them with a PR with the fix (at https://roslyn.codeplex.com/workitem/405 ), but also published a nuget package called DesktopAnalysis that contains all the assemblies for doing C# code analysis, that work on VS2012+ (MSBuild 4.0+): https://www.nuget.org/packages/DesktopAnalysis 我已经为他们提供了一个带有修复程序的PR(在https://roslyn.codeplex.com/workitem/405 ),但也发布了一个名为DesktopAnalysis的nuget包,其中包含用于执行C#代码分析的所有程序集, VS2012 +(MSBuild 4.0+): https ://www.nuget.org/packages/DesktopAnalysis

Just do an install-package DesktopAnalysis -pre instead and you're done. 只需做一个install-package DesktopAnalysis -pre而你就完成了。 The assemblies are the same, the code is the same, etc. 程序集是相同的,代码是相同的,等等。

I'm using this to provide a code migration extension that works from VS2013 all the way to VS2015 Preview. 我正在使用它来提供从VS2013一直到VS2015预览的代码迁移扩展。

您可以分叉Roslyn代码库,并使用定义的MSBUILD12进行编译,但仍然可以使用,但我们并没有真正测试这么多。

This is entirely possible , but not at all easy. 完全有可能 ,但一点也不容易。

You need to make sure that you only ever load the version of the Roslyn assemblies that is in the VS version you're targeting, by removing those assemblies from your VSIX and handling AssemblyResolve to make sure you get the right ones. 您需要确保只加载您所定位的VS版本中的Roslyn程序集版本,方法是从VSIX中删除这些程序集并处理AssemblyResolve以确保获得正确的程序集。
You can see my code that does exactly that here , and you can read more about this technique in my blog post 您可以在此处看到我的代码完全相同,您可以在我的博客文章中阅读有关此技术的更多信息

Note that if you need to [Export] any interfaces defined in Roslyn assemblies, this will not work at all, since MEF will try to load them before you add your handler. 请注意,如果您需要[Export] Roslyn程序集中定义的任何接口,这根本不起作用,因为MEF会在您添加处理程序之前尝试加载它们。 (unless you add a module initializer by hand in ildasm) (除非你在ildasm中手动添加模块初始化程序)

The harder part is that you need to limit yourself to the intersection of the APIs in every Roslyn version you want to support. 更难的部分是您需要将自己限制在要支持的每个Roslyn版本中的API的交集。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM