我使用统一,所以我必须坚持使用.net 2.0。我已将以下路径中的以下文件复制到我的Assets / Editor文件夹中
Ty tyぢty rだぃものrぃものbぃ¥2もの2.0。
Microsoft.Build.Engine.dll
Microsoft.Build.Framework.dll
Microsoft.Build.Tasks.dll
Microsoft.Build.Utilities.dll
我试图找出自己如何做到这一点,但网上的大多数示例使用较新的MSBuild版本,主要是MSBuild 4.0及更高版本。
我尝试了以下代码,但msBuild.BuildEngine为null。
MSBuild msBuild = new MSBuild ();
msBuild.BuildEngine.BuildProjectFile (csproj, new string[] { "Release" }, new Dictionary<string, string> (), new Dictionary<string, string> ());
msBuild.Execute ();
您正在实例化MSBuild
任务,该任务旨在从构建脚本中使用。在该上下文中,它从主机获取引擎,通常是MSBuild可执行文件。
您正在寻找Engine
命名空间中的Microsoft.Build.BuildEngine
类,它位于程序集Microsoft.Build.Engine.dll中。
以下演示控制台应用程序显示了如何使用该类。我创建了一个类MyBuild
,它具有创建引擎和构建项目文件的所有逻辑。
class MyBuild
{
string _proj;
string _target;
bool _result;
public MyBuild(string proj, string target)
{
_proj = proj;
_target= target;
}
public bool Result {get{return _result;}}
public void Start()
{
Engine engine = new Engine();
engine.RegisterLogger(new ConsoleLogger());
_result = engine.BuildProjectFile(_proj, _target);
}
}
在Main方法中我们设置了一些东西。请注意,它还会创建一个Thread并将其ApparatmentState设置为STA。在我的测试中,我没有发现没有问题,但警告是相当持久的,所以我假设有一些情况,如果没有从这样的线程运行它可能会破坏。更安全然后抱歉。
using System;
using System.Threading;
using Microsoft.Build.BuildEngine;
public static void Main()
{
string proj= @"yourprogram.csproj";
string target = "Build";
MyBuild mybuild = new MyBuild(proj, target);
Thread t = new Thread(new ThreadStart(mybuild.Start));
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
if (mybuild.Result)
{
Console.WriteLine("Success!");
}
else
{
Console.WriteLine("Failed!");
}
}
要构建上面的代码,您必须引用Engine.dll作为Framework.dll。
csc program.cs /r:C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Microsoft.Build.Engine.dll /r:C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Microsoft.Build .Framework.dll