gpt4 book ai didi

c# - 以编程方式构建项目

转载 作者:可可西里 更新时间:2023-11-01 07:51:20 26 4
gpt4 key购买 nike

我需要以编程方式为动态创建的 .csproj 构建项目。在谷歌搜索时,我发现了 MS 为 MSBuild 引擎提供的类和 API。有了这些信息,我创建了一个执行 msbuild.exe 然后读取输出的进程,但现在我想使用命名空间 Microsoft.Build.Execution 来构建项目。这是我的程序:

public class Compiler
{
private static string locationOfMSBuilldEXE = "";
public static void Build(string msbuildFileName)
{
BuildManager manager = BuildManager.DefaultBuildManager;

ProjectInstance projectInstance = new ProjectInstance(msbuildFileName);
var result = manager.Build(new BuildParameters()
{
DetailedSummary = true
},
new BuildRequestData(projectInstance, new string[] { "Build" }));
var buildResult = result.ResultsByTarget["Build"];
var buildResultItems = buildResult.Items;

string s = "";
}
}

结果显示构建正常,但我需要知道编译的详细输出以及如何查看它。如果有人能给我链接到一个很好的教程或一本关于 MSBuild 的书,那将非常有帮助。

最佳答案

感谢@ritchmelton。虽然我自己想通了。这是我的代码:我使用了内置记录器 ConsoleLogger

public class Compiler
{
private static string locationOfMSBuilldEXE = "";
public static void Build(string msbuildFileName)
{
ConsoleLogger logger = new ConsoleLogger(LoggerVerbosity.Normal);
BuildManager manager = BuildManager.DefaultBuildManager;

ProjectInstance projectInstance = new ProjectInstance(msbuildFileName);
var result = manager.Build(
new BuildParameters()
{
DetailedSummary = true,
Loggers = new List<ILogger>(){logger}
},
new BuildRequestData(projectInstance, new string[] { "Build" }));
var buildResult = result.ResultsByTarget["Build"];
var buildResultItems = buildResult.Items;

string s = "";
}
}

关于c# - 以编程方式构建项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9942499/

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