Roslyn编译无法解析mscorlib引用

时间:2015-04-08 19:18:26

标签: c# roslyn

我正在开发一个项目,我使用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,Version = 4.0.0.0,Culture = neutral,   公钥= 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,这样我的项目编译包含以下参考:

附注:参考#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与此有关吗?

2 个答案:

答案 0 :(得分:2)

根据Kevin's answer,可以通过指定MSBuildWorkspace的属性来解决此问题:

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

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

答案 1 :(得分:0)

产生错误的项目是否是引用便携式项目的非可移植项目?如果是这样,请观察this answer - 您必须添加外观参考。