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

时间:2014-05-12 20:36:42

标签: c# .net code-analysis roslyn

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

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荧光笔识别出类型名称,编译器抱怨

enter image description here

这是构建日志

   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 ==========

3 个答案:

答案 0 :(得分:10)

所以这个:

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

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

答案 1 :(得分:8)

我找到了this Blog post from Nansen,我应用了修复程序并解决了我的问题。

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

<SpecificVersion>True</SpecificVersion>

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

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

答案 2 :(得分:0)

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

  

类型或命名空间名称&#39; XYZ&#39;找不到(是你吗?   缺少using指令或程序集引用?)

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

<Project DefaultTargets="Configure" xmlns="..." ToolsVersion="12.0">

我按照启动项目的确切语法使用此行更新了它:

<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="...">

事实证明,DefaultTargets属性必须相同。这就是项目参考未包含在&#39; / reference&#39;中的原因。尝试使用CSC.exe命令在运行MSBuild.exe后编译项目时的参数。

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

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