未找到类型或命名空间"是否缺少程序集引用",而所有引用都是正确的

fah*_*ash 12 .net c# code-analysis roslyn

我正在尝试使用MSBuildWorkspace类.我的项目中有所有程序集引用.当我在对象浏览器中打开引用时,我看到了命名空间和我尝试使用的类.但在我的以下使用声明中,

using Microsoft.CodeAnalysis.MSBuild

我得到了一个

The type or namespace name 'MSBuild' does not exist in the namespace 'Microsoft.CodeAnalysis' (are you missing an assembly reference?)
Run Code Online (Sandbox Code Playgroud)

但有趣的是,Syntax荧光笔识别出类型名称,它的编译器抱怨

在此输入图像描述

这是构建日志

   1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "Microsoft.CodeAnalysis.Workspaces" could not be resolved because it has an indirect dependency on the .NET Framework assembly "Microsoft.Build, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which has a higher version "12.0.0.0" than the version "4.0.0.0" in the current target framework.
    1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "Microsoft.CodeAnalysis.VisualBasic.Workspaces" could not be resolved because it has an indirect dependency on the .NET Framework assembly "Microsoft.Build, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which has a higher version "12.0.0.0" than the version "4.0.0.0" in the current target framework.
    1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "Microsoft.CodeAnalysis.CSharp.Workspaces" could not be resolved because it has an indirect dependency on the .NET Framework assembly "Microsoft.Build, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which has a higher version "12.0.0.0" than the version "4.0.0.0" in the current target framework.
    1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "Microsoft.CodeAnalysis.Workspaces" could not be resolved because it has an indirect dependency on the .NET Framework assembly "Microsoft.Build.Framework, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which has a higher version "12.0.0.0" than the version "4.0.0.0" in the current target framework.
    1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "Microsoft.CodeAnalysis.VisualBasic.Workspaces" could not be resolved because it has an indirect dependency on the .NET Framework assembly "Microsoft.Build.Framework, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which has a higher version "12.0.0.0" than the version "4.0.0.0" in the current target framework.
    1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference "Microsoft.CodeAnalysis.CSharp.Workspaces" could not be resolved because it has an indirect dependency on the .NET Framework assembly "Microsoft.Build.Framework, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which has a higher version "12.0.0.0" than the version "4.0.0.0" in the current target framework.
    1>c:\users\fahadash\documents\visual studio 2012\Projects\RoslynEditor\RoslynEditor\MainWindow.xaml.cs(37,36,37,43): error CS0234: The type or namespace name 'MSBuild' does not exist in the namespace 'Microsoft.CodeAnalysis' (are you missing an assembly reference?)
    1>c:\users\fahadash\documents\visual studio 2012\Projects\RoslynEditor\RoslynEditor\MainWindow.xaml.cs(37,96,37,103): error CS0234: The type or namespace name 'MSBuild' does not exist in the namespace 'Microsoft.CodeAnalysis' (are you missing an assembly reference?)
    ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Run Code Online (Sandbox Code Playgroud)

Jas*_*ski 10

所以这:

警告MSB3258:无法解析主要参考"Microsoft.CodeAnalysis.Workspaces",因为它对.NET Framework程序集"Microsoft.Build,Version = 12.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a"具有间接依赖性比当前目标框架中的版本"4.0.0.0"更高版本"12.0.0.0".

意味着您正在使用面向.NET 4.0框架的项目构建.您应该使用Visual Studio 2013定位4.5.1.其他配置不受支持.我不建议试图通过消除警告来"强制"这一点 - 这可能只会导致问题.Roslyn使用4.5中添加的API,因此您可能会遇到麻烦,试图解决问题.

  • 此错误必须有另一个原因.我正在使用Microsoft.Build.Framwork,我在所有项目中使用vs2013定位4.5.1,基于tfs2013构建. (3认同)
  • 不,我们不是为了赚更多钱而这样做.当我们处理Roslyn时,我们需要依赖于框架和Visual Studio的新组件来执行一些非常棒的新功能.维护既允许我们定位新功能的构建_and_也仍然适用于旧版本变得比它的价值更麻烦. (2认同)

fah*_*ash 8

从Nansen找到了这篇博客文章,我应用了修复程序并解决了我的问题.

解决方案摘要: 在XML编辑器中编辑csproj文件,找到令您不安的引用的元素,并将以下子元素添加到这些元素中.

<SpecificVersion>True</SpecificVersion>

确保单词True只是首字母大写(True,不是true或TRUE).

在VS中保存并重新加载项目并构建它.

  • 汇总和/或从外部资源中提取相关信息.如果链接/博客无法访问,人们(阅读此"答案")将不知道提议的解决方案的关键是使用特定版本.. (2认同)