简体   繁体   English

Roslyn编译无法解析mscorlib引用

[英]Roslyn compilation doesn't resolve mscorlib references

I'm working on a project where I compile projects from a solution using 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);
    // ...

Compilation contains errors such as 编译包含以下错误:

error CS0012: The type 'Task' is defined in an assembly that is not referenced. 错误CS0012:在未引用的程序集中定义了“任务”类型。 You must add a reference to assembly 'System.Threading.Tasks, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. 您必须添加对程序集'System.Threading.Tasks,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'的引用。

Why Roslyn won't accept an existing reference to mscorlib? 为什么Roslyn不接受对mscorlib的现有引用?

Are there some CompilationOptions that I should consider? 我应该考虑一些CompilationOptions吗? Per this thread I tried assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default but it didn't help. 这个线程中,我尝试了assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default但是它没有帮助。 I tried to work with metadataReferenceResolver but couldn't find much information about it. 我尝试使用metadataReferenceResolver但找不到太多信息。

Following the solution in Roslyn has no reference to System.Runtime I implemented code that ensures that a project has references to mscorlib.dll, System.Core.dll, System.dll and System.Runtime.dll, such that my project and compilation have the references: 遵循Roslyn中的解决方案后,我没有引用System.Runtime。我实现了代码,以确保项目引用了mscorlib.dll,System.Core.dll,System.dll和System.Runtime.dll,从而使我的项目编译具有参考:

Side note: Reference #7 has been added this way. 旁注:参考号7已通过这种方式添加。 The project already had references #1, 2 and 3, and removing them and replacing with ones from C:\\Windows\\Microsoft.NET\\Framework didn't solve the issue. 该项目已经具有引用#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
  • How can I get Roslyn to compile this project? 如何让罗斯林编译该项目?
  • Are there any CompilationOptions I should use? 我应该使用任何CompilationOptions吗?
  • Is Roslyn's issue #970 related to this? 罗斯林的#970问题与此有关吗?

As per Kevin's answer , this can be resolved by specifying a property for MSBuildWorkspace : 根据Kevin的 回答 ,可以通过为MSBuildWorkspace指定一个属性来解决此问题:

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

Now solutions opened in msWorkspace will correctly resolve their references. 现在,在msWorkspace打开的解决方案将正确解析其引用。

Is the project that is producing the errors a non-portable project that is referencing a portable project? 产生错误的项目是否是引用可移植项目的非便携式项目? If so, observe this answer -- you'll have to add facade references. 如果是这样,请观察此答案 -您必须添加外观参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM