我正在使用Unity,因此我必须坚持.net 2.0。我已将以下文件从以下路径复制到我的资产/编辑文件夹
Unity editor data nomo lib mono 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
任务,该任务应在buildscript中使用。在这种情况下,它从主机中获取引擎,通常是MSBuild可执行的。
您正在寻找在Microsoft.Build.BuildEngine
名称空间中找到的Engine
类,并位于汇编中Microsoft.build.engine.dll。
以下演示控制台应用程序显示了如何使用该类。我创建了一个MyBuild
类,该类具有创建引擎并构建ProjectFile的所有逻辑。
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);
}
}
在主要方法中,我们设置了问题。请注意,它还创建了一个线程并将其设备设置为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.gine.dll/r:c: windows Microsoft.net.net framework64 v2.0.50727 microsoft.build.framework.dll