gpt4 book ai didi

c# - Roslyn 编译不解析 mscorlib 引用

转载 作者:太空狗 更新时间:2023-10-29 19:45:16 31 4
gpt4 key购买 nike

我正在开发一个项目,在该项目中,我使用 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);
// ...

编译包含错误如

error CS0012: The type 'Task' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Threading.Tasks, Version=4.0.0.0, Culture=neutral, 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 的代码,这样我的项目编译 有引用文献:

旁注:引用#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与此有关?

最佳答案

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

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

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

关于c# - Roslyn 编译不解析 mscorlib 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29523473/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com