我正在处理一个项目,使用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的现有引用?
- System.Threading.Task的MSDN页面指出该类型确实位于mscorlib.dll中
- PublicKeyToken b03f5f7f11d50a3a与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与此相关吗?