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

2023-09-03 03:24:49 作者:深碍久绊

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

使用Microsoft。codeAnalysis.MSBuild

我得到一个

 类型或命名空间名称'的MSBuild'没有在命名空间'微软。codeAnalysis存在(是否缺少程序集引用?)
 

不过,有趣的是语法高亮识别的类型名称,它的编译器抱怨

下面是生成日志

  1> C:\ WINDOWS \ Microsoft.NET \框架\ v4.0.30319 \ Microsoft.Common.targets(1605,5):警告MSB3258:主要参考微软$。 C $ cAnalysis.Workspaces无法得到解决,因为它在.NET Framework组件中的间接依赖Micr​​osoft.Build,版本= 12.0.0.0,文化=中性公钥= b03f5f7f11d50a3a,它具有更高版本12.0.0.0 比版本在当前目标框架4.0.0.0。
    1> C:\ WINDOWS \ Microsoft.NET \框架\ v4.0.30319 \ Microsoft.Common.targets(1605,5):警告MSB3258:主要参考微软codeAnalysis.VisualBasic.Workspaces。无法解析因为它在.NET Framework组件中的间接依赖Microsoft.Build,版本= 12.0.0.0,文化=中性公钥= b03f5f7f11d50a3a里面有比版本4.0.0.0高版本12.0.0.0当前目标框架。
    1> C:\ WINDOWS \ Microsoft.NET \框架\ v4.0.30319 \ Microsoft.Common.targets(1605,5):警告MSB3258:主要参考微软codeAnalysis.CSharp.Workspaces。无法解析因为它在.NET Framework组件中的间接依赖Microsoft.Build,版本= 12.0.0.0,文化=中性公钥= b03f5f7f11d50a3a里面有比版本4.0.0.0高版本12.0.0.0当前目标框架。
    1> C:\ WINDOWS \ Microsoft.NET \框架\ v4.0.30319 \ Microsoft.Common.targets(1605,5):警告MSB3258:主要的参考。微软codeAnalysis.Workspaces无法得到解决,因为它对.NET框架组件中的间接依赖Microsoft.Build.Framework,版本= 12.0.0.0,文化=中性公钥= b03f5f7f11d50a3a里面有比版本4.0.0.0高版本12.0.0.0当前目标框架。
    1> C:\ WINDOWS \ Microsoft.NET \框架\ v4.0.30319 \ Microsoft.Common.targets(1605,5):警告MSB3258:主要参考微软codeAnalysis.VisualBasic.Workspaces。无法解析因为它在.NET Framework组件中的间接依赖Microsoft.Build.Framework,版本= 12.0.0.0,文化=中性公钥= b03f5f7f11d50a3a,它具有比版本4.0.0.0更高版本的12.0.0.0 在当前的目标框架。
    1> C:\ WINDOWS \ Microsoft.NET \框架\ v4.0.30319 \ Microsoft.Common.targets(1605,5):警告MSB3258:主要参考微软codeAnalysis.CSharp.Workspaces。无法解析因为它在.NET Framework组件中的间接依赖Microsoft.Build.Framework,版本= 12.0.0.0,文化=中性公钥= b03f5f7f11d50a3a,它具有比版本4.0.0.0更高版本的12.0.0.0 在当前的目标框架。
    1> C:\用户\ fahadash \文档\的Visual Studio 2012 \项目\ RoslynEditor \ RoslynEditor \ MainWindow.xaml.cs(37,36,37,43):错误CS0234:类型或命名空间名称'的MSBuild'不存在在命名空间'微软。codeAnalysis'(是否缺少程序集引用?)
    1> C:\用户\ fahadash \文档\的Visual Studio 2012 \项目\ RoslynEditor \ RoslynEditor \ MainWindow.xaml.cs(37,96,37,103):错误CS0234:类型或命名空间名称'的MSBuild'没有在存在命名空间微软。codeAnalysis'(是否缺少程序集引用?)
    ==========全部重新生成:0成功,1失败,0跳过==========
 
找不到类型或命名空间名称 是否缺少using指令或程序集引用

解决方案

所以这样的:

警告MSB3258:主要参考。微软codeAnalysis.Workspaces无法得到解决,因为它在.NET Framework组件Microsoft.Build,版本= 12.0.0.0间接依赖,文化=中立,公钥= b03f5f7f11d50a3a,它具有更高版本在当前目标框架12.0.0.0比版本4.0.0.0。

意味着你正在与你的项目面向.NET 4.0框架。你应该针对4.5.1与Visual Studio 2013的其他配置是不支持的。我不建议试图逼这个由沉默的警告 - 这可以直接导致问题的道路。罗斯林使用在4.5中加入的API,所以你将有麻烦试图平息这一问题。

I am trying to use MSBuildWorkspace class . I have all assembly references in my project. When I open the reference in object browser, I see the namespace and the class I am trying to use. But in my following using statement,

using Microsoft.CodeAnalysis.MSBuild

I am getting a

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

But funny that Syntax highlighter recognizes the type name, its the compiler complaining

Here is the build log

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

解决方案

So this:

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.

Means you are building with your project targeting the .NET 4.0 framework. You should be targeting 4.5.1 with Visual Studio 2013. Other configurations are unsupported. I do not recommend trying to "force" this by silencing the warning -- that can just cause issues down the road. Roslyn uses APIs added in 4.5, so you will have troubles trying to silence the issue.