奇怪的诊断错误,预定义类型系统...未定义或导入

Strange diagnostics errors, prefedined type System... is not defined or imported

我在解析非常基本的 .NET 4.6 应用程序时收到 Roslyn 诊断错误。可以从那里下载解决方案文件 https://github.com/dotnet/roslyn/files/2393288/DemoSolution.zip

依赖关系树如下所示:

BLL -> DB

我在 BLL 项目中遇到以下诊断错误:

解决方案和项目构建良好,Roslyn 仍然给出这些错误。也许错误具有误导性,我需要以某种方式配置项目?知道如何解决这些错误吗?

这是用于解析文件的代码:

var properties = new Dictionary<string, string>
                {
                    ["DesignTimeBuild"] = "true",
                    ["CheckForSystemRuntimeDependency"] = "true"
                };
                var workspace = MSBuildWorkspace.Create(properties);

                workspace.WorkspaceFailed += (sender, args) =>
                {

                };
                workspace.LoadMetadataForReferencedProjects = true;

Solution solution = workspace.OpenSolutionAsync(SolutionFilePath).Result;               
                    foreach (var p in solution.Projects)
                    {   
                        foreach (var file in p.Documents)
                        {
      var semanticModel = file.GetSemanticModelAsync().Result;

                        var mscorlib = MetadataReference.CreateFromFile(file.FilePath);
                    var compilation = CSharpCompilation.Create("MyCompilation",
                        new[] { semanticModel.SyntaxTree }, new[] { mscorlib });

                    var model = compilation.GetSemanticModel(semanticModel.SyntaxTree);
                    var declarationDiagnistics = model.Compilation.GetDeclarationDiagnostics(CancellationToken.None);
                    var parseDiagnostics = model.Compilation.GetParseDiagnostics(CancellationToken.None);
                    var allDiagnostics = model.Compilation.GetDiagnostics(CancellationToken.None);
                    var methodBodyDiagnostics = model.Compilation.GetMethodBodyDiagnostics(CancellationToken.None);

                                  }
                }

订阅 workspace.workspaceFailed 事件导致以下错误:

Msbuild failed when processing the file 'MYPATH\BLL.csproj' with message: C:\Program Files (x86)\Microsoft Visual Studio17\Enterprise\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets: (1656, 5): The "GetReferenceNearestTargetFrameworkTask" task could not be instantiated from the assembly "C:\Program Files (x86)\Microsoft Visual Studio17\Enterprise\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.Build.Tasks.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'NuGet.Build.Tasks.GetReferenceNearestTargetFrameworkTask' to type 'Microsoft.Build.Framework.ITask'. C:\Program Files (x86)\Microsoft Visual Studio17\Enterprise\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets: (1656, 5): The "GetReferenceNearestTargetFrameworkTask" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.

所以这终于解决了它:

  1. 按照@GeorgeAlexandria 的建议添加了 microsoft.build 重定向 http://github.com/Microsoft/msbuild/issues/2369#issuecomment-353674937

  2. 已从输出 bin 文件夹中清除 Microsoft.Build.*

  3. 添加了 Microsoft.Build.定位器作为参考

  4. 在工作区代码上方添加了行 MSBuildLocator.RegisterDefaults()。

来源: https://github.com/dotnet/roslyn/issues/26029#issuecomment-380164421