1

I am using unity so I have to stick with .net 2.0. I have copied the following files from the following path to my Assets/Editor folder

Unity\Editor\Data\Mono\lib\mono\2.0\

Microsoft.Build.Engine.dll

Microsoft.Build.Framework.dll

Microsoft.Build.Tasks.dll

Microsoft.Build.Utilities.dll

I tried to figure out my self how to do it, but most examples on the web uses newer MSBuild version, mostly MSBuild 4.0 and up.

I tried the following code, but the msBuild.BuildEngine is null.

MSBuild msBuild = new MSBuild ();
msBuild.BuildEngine.BuildProjectFile (csproj, new string[] { "Release" }, new Dictionary<string, string> (), new Dictionary<string, string> ());
msBuild.Execute ();
4
  • If your goal is to use Visual Studio with Unity you can just use Visual Studio tools for unity: msdn.microsoft.com/en-au/library/dn940019.aspx Commented Oct 3, 2017 at 1:31
  • @tweetypi Thanks but that's not the goal. I want to dynamically build a related csproj needed by my game. That way I will not have to build my csproj then build my game, and it will be a streamlined process. Commented Oct 3, 2017 at 1:47
  • 1
    With Unity 2017, you can enable support for .NET 4.6+C# 6. Go to Edit->Project Settings->Player and set Other Settings->Scripting Runtime Version. Commented Oct 3, 2017 at 2:06
  • @DavidOliver Exactly, I tried and it worked. But it's experimental and I am doing the game in a production environment, so that's not an option for now. Commented Oct 3, 2017 at 2:37

1 Answer 1

1

You are instantiating the MSBuild task which is meant to be used from within a buildscript. In that context it gets the Engine from the host, most often the MSBuild executable.

You're looking for the Engine class that is found in the Microsoft.Build.BuildEngine namespace and sits in the assembly Microsoft.Build.Engine.dll .

The following Demo Console application shows how you can use that class. I've created a class MyBuild that has all the logic to create the Engine and build a 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);
    }
}

In the Main method we set things up. Notice that it also creates a Thread and sets its ApparatmentState to STA. In my testing I found no issues when not doing that but the warning was rather persistent so I assume there are scenario's where it might break if not run from such thread. Better safe then sorry.

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!");
    }
}

To build above code you have to reference both the Engine.dll as the 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.