【问题标题】:Type or namespace not found "are you missing assembly reference" while all references are correct未找到类型或命名空间“您是否缺少程序集引用”,而所有引用均正确
【发布时间】:2014-05-12 20:36:41
【问题描述】:

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

using Microsoft.CodeAnalysis.MSBuild

我得到了一个

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

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

这是构建日志

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

【问题讨论】:

  • 您是否尝试过重建/清理/删除程序集引用并重新添加?
  • 您是否针对框架 4.5 并将其设置为客户端配置文件?类似的东西......?
  • 设置为使用4.5。我没有看到 4.5 客户端配置文件作为选项。我尝试了 4 个客户端配置文件,现在它甚至无法识别 CodeAnalysis 是否位于 Microsoft 命名空间中。
  • 我们不支持客户资料。除了错误之外,请在您的构建日志中包含来自编译器的任何警告。
  • @JasonMalinowski 我刚刚做了

标签: c# .net code-analysis roslyn


【解决方案1】:

所以这个:

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

【讨论】:

  • 设置为 4.5。我从来没有上过 4。我使用我的 VS2012 创建了一个新的 WPF 应用程序,它默认为 4.5。然后我通过 Nuget 添加 Microsoft.CodeAnalysis ,我仍然得到这个错误。如果你们逼着我们向微软支付更多的钱并升级到 2013 年,这是不公平的。
  • 不,我们这样做不是为了赚更多的钱。当我们在 Roslyn 上工作时,我们需要依赖框架和 Visual Studio 的更新组件来完成一些很棒的新功能。维护既能让我们针对新功能并且仍然适用于旧版本的构建变得比它的价值更麻烦。
  • 如果您不尝试扩展 Visual Studio,速成版应该足以满足您的需求。您还可以尝试安装将安装 12.0 程序集的 MSBuild standalone package,然后执行 SpecificVersion 尝试强制 VS 使用 12.0 程序集。我还没有尝试过,所以我不能完全保证它是否会起作用。
  • @JasonMalinowski,你能在下面评论 fahadash 的回答吗?它似乎工作得很好,我试图使用 Microsoft.CodeAnalysis 的一切似乎都有效。 (目标是在 VS 2012 中使用 Microsoft.CodeAnalysis)由于我在任何一种情况下都针对 .Net Framework 4.5.1,我很好奇为什么使用 VS 2012 会充满危险?
  • 这个错误一定有其他原因。我正在使用 Microsoft.Build.Framwork 来体验它,并且我在所有项目中都使用了针对 4.5.1 的 vs2013,基于 tfs2013。
【解决方案2】:

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

解决方案总结: 在 XML 编辑器中编辑 csproj 文件,找到困扰您的引用元素,并将以下子元素添加到这些元素中。

<SpecificVersion>True</SpecificVersion>

确保单词 True 仅是第一个大写字母(True、not true 或 TRUE)。

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

【讨论】:

  • 我知道你在另一个博客上找到了答案。但是,您能否在您所做的事情中为您的答案添加更多细节。以防万一网站将来死了,答案就在这里。
  • 从外部资源中总结和/或提取相关信息。如果链接/博客无法访问,人们(阅读此“答案”)将不知道建议解决方案的关键是使用特定版本..
  • 刚刚做了。感谢您提出。
  • 这对我有用,因为另一个答案不正确,因为我已经针对 4.5.1。
【解决方案3】:

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

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

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

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

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

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

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

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

顺便说一句,在尝试解决构建错误时,将 PSBuild(MSBuild 的 PowerShell 接口)与 Markdown Pad 2 结合使用在本地效果很好。

【讨论】:

    猜你喜欢
    • 2011-02-12
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 2020-11-28
    相关资源
    最近更新 更多