无法加载类型 Microsoft.Build.Framework.SdkResultItem

Could not load type Microsoft.Build.Framework.SdkResultItem

我有一个应用程序使用 Roslyn API 来分析分离解决方案中的 C# 代码;在一些 Visual Studio 更新后,我在我的应用程序中收到此错误:

Msbuild failed when processing the file '{separated solution .csproj file}' with message: Could not load type 'Microsoft.Build.Framework.SdkResultItem' from assembly 'Microsoft.Build.Framework, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

分析解决方案中的每个项目都会显示此错误;我可以在变量 ws.Diagnostics 中看到错误:

using var ws = MSBuildWorkspace.Create();

ws.LoadMetadataForReferencedProjects = true;

var solution = await ws.OpenSolutionAsync(solutionPath,
    msbuildLogger: buildLogger,
    cancellationToken: cancellationToken);

在 Visual Studio 更新之前,我的代码可以正常工作;有一些方法可以在不重新安装 Visual Studio?

的情况下修复它

我已经尝试使用 开发人员命令提示符 重新安装 Microsoft.Build.Framework 参考,但没有用。

按照 this site 中的说明使用 Microsoft.Build.Locator,并在调用 MSBuildWorkspace.Create 之前执行此操作。这将确保您具有绑定重定向,使您的版本独立。