ProjectReferences存在时,不会填充项目MetadataReferences

我正在加载MSBuildWorkspace中的解决方案:

var msWorkspace = MSBuildWorkspace.Create(); var solution = msWorkspace.OpenSolutionAsync(solutionPath).Result; 

没有ProjectReferences的项目显示所有MetadataReferences,包括mscorlib。 具有ProjectReferences的项目具有MetadataReferences的空集合。

由于编译工作,我想编译器平台由于某种原因没有填充这个集合,我想知道为什么? 如果我删除ProjectReferences,则会正确填充MetadataReferences集合。

编辑:诊断包含缺少mscorlib类型(如Object和DateTime)的错误,因此项目似乎无法编译,因为这些缺少引用。

在尝试打开解决方案后,我最终通过检查MSBuildWorkspace对象上的.Diagnostics来了解发生了什么:

 [Failure] Msbuild failed when processing the file 'c:\xxx\someproject.csproj' with message: C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\Microsoft.CSharp.Core.targets: (84, 5): The "Csc" task could not be instantiated from the assembly "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\Microsoft.Build.Tasks.CodeAnalysis.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'Microsoft.CodeAnalysis.BuildTasks.Csc' to type 'Microsoft.Build.Framework.ITask'. 

似乎正在使用不同版本的MSBuild。 添加程序集重定向(从C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe.config )到我的项目app.config解决了它:

                                  

现在,解决方案中的每个项目都正确填充了所有引用。

我在VS2017 15.7.1上并且针对.NET 4.7.1,因此在Microsoft.CodeAnalysis软件包之上不需要NuGet包(我使用的是2.8.0)。