【问题标题】:Roslyn compilation doesn't resolve mscorlib referencesRoslyn 编译无法解析 mscorlib 引用
【发布时间】:2015-04-08 19:18:26
【问题描述】:

我正在开发一个项目,我使用 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:类型“任务”是在未定义的程序集中定义的 参考。您必须添加对程序集的引用 'System.Threading.Tasks,版本=4.0.0.0,文化=中性, PublicKeyToken=b03f5f7f11d50a3a'。

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

我应该考虑一些CompilationOptions 吗? 每this thread 我试过assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default 但它没有帮助。我尝试与metadataReferenceResolver 合作,但找不到太多相关信息。

按照Roslyn has no reference to System.Runtime 中的解决方案,我实现了确保项目具有对 mscorlib.dll、System.Core.dll、System.dll 和 System.Runtime.dll 的引用的代码,这样我的 项目compilation 有引用:

旁注:参考 #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's issue #970 与此有关吗?

【问题讨论】:

  • Resembles this 也是。它仍然是 beta 质量,通过添加参考来跛行。它位于 Facades 子目录中。
  • 是的,它与此类似,但此解决方案无法解决问题。我将尝试发布一个发生此问题的最小解决方案。

标签: c# roslyn


【解决方案1】:

根据Kevin'sanswer,这可以通过为MSBuildWorkspace指定一个属性来解决:

var props = new Dictionary<string, string>();
props["CheckForSystemRuntimeDependency"] = "true";
var msWorkspace = MSBuildWorkspace.Create(props);

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

【讨论】:

  • 这对我不起作用,我使用的是 VS 2015 RC。任何人?有更新吗?
【解决方案2】:

产生错误的项目是引用可移植项目的非可移植项目吗?如果是这样,请注意this answer——您必须添加外观引用。

【讨论】:

  • 不,解决方案中的两个项目都是面向 .NET 4.5 的类库
猜你喜欢
  • 1970-01-01
  • 2016-01-31
  • 2016-06-13
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2017-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多