以编程方式构建项目 [英] Building programmatically a project

查看:87
本文介绍了以编程方式构建项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to build a project programmatically for a .csproj I am creating on the fly. While searching Google I found the classes and API provided by the MS for the MSBuild Engine. With that information, I create a process which executes msbuild.exe and then reads the output, but now I want to use the namespace Microsoft.Build.Execution to build the project. This is my program:

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的好教程或书,那将非常有帮助.

The results show that this is building fine, but I need to know the detailed output from the compile and how to view it. It would be really helpful if someone can give me link to a good tutorial or a book on MSBuild.

推荐答案

您需要将实现ILogger接口的类的实例添加到BuildParameters.您可以在Microsft.Build.Logging命名空间中添加提供的记录器之一的新实例,也可以自己实现ILogger,因为它很小,并且在Microsoft.Build.Utilities命名空间中有一个名为Logger的帮助程序类,该类是易于扩展.

You need to add a instance of a class that implements the ILogger interface to your BuildParameters. You can add a new instance of one of the supplied loggers in the Microsft.Build.Logging namespace, or you can implement ILogger yourself as it is very small and there is a helper class in the Microsoft.Build.Utilities namespace called Logger that is easy to extend.

构建记录器

ILogger界面

记录器帮助程序

这篇关于以编程方式构建项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆