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

我正在尝试使用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?) 

但有趣的是,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 ========== 

所以这:

警告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,因此您可能会遇到麻烦,试图解决问题。

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

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

True

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

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

在我的启动项目中使用项目引用时,我从构建服务器生成了相同的构建错误消息 – 它在通过VS2013 IDE构建时工作正常:

找不到类型或命名空间名称’XYZ’(您是否缺少using指令或程序集引用?)

在本地运行MSBuild.exe而不是在构建服务器中运行后,我能够复制错误。 通过从解决方案中卸载它来检查.csproj文件以获取项目引用,我注意到我在顶部有以下行:

  

我按照启动项目的确切语法更新了这一行:

  

事实certificate,DefaultTargets属性必须相同。 这就是为什么在运行MSBuild.exe后尝试使用CSC.exe命令编译项目时,项目引用未包含在’/ reference’参数中的原因。

我花了一段时间才找到解决这个问题的方法,因为我在任何论坛上都找不到类似的东西。 这是一个遗留应用程序,它始终在本地而不是在构建服务器中构建。

顺便说一句,在尝试解决构建错误时,使用PSBuild(与MSBuild的PowerShell接口)以及Markdown Pad 2在本地工作得很好。