我正在尝试使用 Microsoft.Build 中的类构建一个项目。
代码是:
var project = new ProjectInstance(CS_PROJ_FILE);
project.Build();
但是它抛出以下异常:
Microsoft.Build.Shared.InternalErrorException occurred
HResult=0x80131500
Message=MSB0001: Internal MSBuild Error: Type information for Microsoft.Build.Utilities.ToolLocationHelper was present in the whitelist cache as Microsoft.Build.Utilities.ToolLocationHelper, Microsoft.Build.Utilities.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a but the type could not be loaded. unexpectedly null
Source=Microsoft.Build
我尝试将以下内容添加到包中(在 net452 和 net7 项目中):
- id="Microsoft.Build"版本="15.1.1012"
- id="Microsoft.Build.Framework"版本="15.1.1012"
- id="Microsoft.Build.Runtime"version="15.1.1012"
- id="Microsoft.Build.Tasks.Core"版本="15.1.1012"
- id="Microsoft.Build.Utilities.Core"version="15.1.1012"
仍然得到相同的结果。
我也尝试过使用 BuildManager
,如下所示:
var buildManager = new BuildManager();
buildManager.Build(new BuildParameters(),
new BuildRequestData(new ProjectInstance(CS_PROJ_FILE),
new[] {"Build"}));
最佳答案
安装后我遇到了同样的错误:
Install-Package Microsoft.Build -Version 15.1.1012
但后来我安装了:
Install-Package Microsoft.Build.Utilities.Core -Version 15.1.1012
事情开始起作用。
有点困惑...
“dasMulli”向我指出了这个 stackoverflow 问题:
关于c# - 使用 Microsoft.Build API 构建项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44249320/