Roslyn编译器无法解析mscorlib引用

8
我正在处理一个项目,使用Roslyn编译解决方案中的项目。
foreach (var projectId in solution.GetProjectDependencyGraph().GetTopologicallySortedProjects())
{
    var project = solution.GetProject(projectId);
    var compilation = project.GetCompilationAsync().Result;
    var errors = compilation.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error);
    // ...

编译出错,例如:

错误 CS0012: 类型 'Task' 在未引用程序集中被定义。必须向程序集“System.Threading.Tasks, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”添加引用。

为什么Roslyn不接受对mscorlib的现有引用?

是否有一些需要考虑的CompilationOptions?根据这个帖子,我尝试了assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default,但没有帮助。我尝试使用metadataReferenceResolver,但找不到太多关于它的信息。

按照Roslyn没有对System.Runtime的引用中的解决方案,我实现了代码,确保项目引用了mscorlib.dll、System.Core.dll、System.dll和System.Runtime.dll,使得我的项目编译都有这些引用:

顺便说一句:引用#7是这样添加的。该项目已经有了引用#1、2和3,将它们移除并替换为来自C:\Windows\Microsoft.NET\Framework的引用并没有解决问题。

project.MetadataReferences.ToList()
Count = 8
    [0]: Assembly Path='C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll'
    [1]: Assembly Path='C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll'
    [2]: Assembly Path='C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Core.dll'
    [3]: Assembly Path='C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.dll'
    [4]: Assembly Path='C:\Users\Amadeus\Documents\GitHub\InterProcessQueue\src\MemoryMappedQueue\packages\xunit.runner.visualstudio.2.0.0-rc1-build1030\build\_common\xunit.abstractions.dll'
    [5]: Assembly Path='C:\Users\Amadeus\Documents\GitHub\InterProcessQueue\src\MemoryMappedQueue\packages\xunit.assert.2.0.0-rc1-build2826\lib\portable-net45+aspnetcore50+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.assert.dll'
    [6]: Assembly Path='C:\Users\Amadeus\Documents\GitHub\InterProcessQueue\src\MemoryMappedQueue\packages\xunit.extensibility.core.2.0.0-rc1-build2826\lib\portable-net45+aspnetcore50+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.dll'
    [7]: Assembly Path='C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Runtime.dll'

compilation.ExternalReferences.ToList()
Count = 9
    [0]: Assembly Path='C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll'
    [1]: Assembly Path='C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll'
    [2]: Assembly Path='C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Core.dll'
    [3]: Assembly Path='C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.dll'
    [4]: Assembly Path='C:\Users\Amadeus\Documents\GitHub\InterProcessQueue\src\MemoryMappedQueue\packages\xunit.runner.visualstudio.2.0.0-rc1-build1030\build\_common\xunit.abstractions.dll'
    [5]: Assembly Path='C:\Users\Amadeus\Documents\GitHub\InterProcessQueue\src\MemoryMappedQueue\packages\xunit.assert.2.0.0-rc1-build2826\lib\portable-net45+aspnetcore50+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.assert.dll'
    [6]: Assembly Path='C:\Users\Amadeus\Documents\GitHub\InterProcessQueue\src\MemoryMappedQueue\packages\xunit.extensibility.core.2.0.0-rc1-build2826\lib\portable-net45+aspnetcore50+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.dll'
    [7]: Assembly Path='C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Runtime.dll'
    [8]: Compilation (C#): MemoryMappedQueue
  • 我该如何让Roslyn编译这个项目?
  • 是否应该使用任何CompilationOptions
  • Roslyn的问题#970与此相关吗?

这也类似于此。它仍处于测试质量阶段,通过添加引用来支持。它位于“Facades”子目录中。 - Hans Passant
是的,它看起来像这样,但这个解决方案没有解决问题。我会尝试发布一个最小化的解决方案,以便出现此问题。 - Amadeusz Wieczorek
2个回答

2
根据Kevin回答,这可以通过为MSBuildWorkspace指定属性来解决:
var props = new Dictionary<string, string>();
props["CheckForSystemRuntimeDependency"] = "true";
var msWorkspace = MSBuildWorkspace.Create(props);

现在在 msWorkspace 中打开的解决方案将正确解析它们的引用。

3
对我来说那个不起作用,我正在使用VS 2015 RC。有人能提供帮助吗?有更新吗? - Allan.C

0

产生错误的项目是否是引用可移植项目的非可移植项目?如果是,请参考this answer -- 您需要添加门面引用。


不,解决方案中的两个项目都是针对.NET 4.5的类库。 - Amadeusz Wieczorek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接