以编程方式构建项目 [英] Building programmatically a project
问题描述
我需要以编程方式为正在动态创建的.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.
这篇关于以编程方式构建项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!