编程建立项目 [英] Building programatically a project

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

问题描述

我需要以编程方式生成一个项目一个.csproj的我创造的飞行。
在谷歌搜索,我发现了MS的MSBuild引擎提供的类和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 命名空间的辅助类日志这很容易扩展。

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天全站免登陆