4

I have a target project that uses c# 6.0 I need to programatically build it.

I have the code below:

        var pc = new ProjectCollection();
        pc.DefaultToolsVersion = "14.0" //set tools version
        var globalProperty = new Dictionary<string, string>
        {
            {"Configuration", "Release"},
            {"Platform", "Any CPU"},
            {"OutputPath", Utils.GetGaugeBinDir()}
        };

        var buildRequestData = new BuildRequestData(solutionFullPath, globalProperty, "14.0", new[] {"Build"}, null); //Set tools version here as well

        var errorCodeAggregator = new ErrorCodeAggregator();
        var buildParameters = new BuildParameters(pc) {Loggers = new ILogger[] {consoleLogger, errorCodeAggregator}};

        var buildResult = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequestData);

No matter where I set the tools version (of the two options above), it does not build C# 6.0.

On command line, I can do this:

msbuild foo.csproj /tv:14.0 /t:rebuild

I invoke this from MSBuild 12.0 bin directory, and it works. If I drop the /tv:14.0 flag, it fails as expected.

So, question is, what is the programatic way of specifying /tv flag to BuildManager ?

2
  • I'm struggling with the same issue. Did you figure out a solution? Currently the log file from my build suggests it is unable to build C# 6.0 syntax. I'm setting both the DefaultToolsVersion of the ProjectCollection instance and the BuildRequestData paramter as 14.0. Clearly that's wrong (or insufficient). Commented Nov 2, 2016 at 17:53
  • @AlexanderHøst - I'm afraid not. I have two ways forward to fix this: (1) upgrade my code to use MSBuild 14.0 or (2) invoke msbuild process and pass the relevant parameters. I don't like either very much, so am leaving it as it is for now :( Commented Nov 4, 2016 at 6:20

1 Answer 1

1
var buildRequest = new BuildRequestData(_solutionPath, globalProperties, null, new[] {"Build"},

When creating your BuildRequestData, pass null for the toolsVersion.

Then make sure the Microsoft.Build*.dll's referenced by your project are the correct version. By default, VS will add the ones from inside it's own install directory. The updated ones should exist at "C:\Program Files (x86)\MSBuild\14.0\Bin"

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

1 Comment

The context is that I want to be able to invoke the build against one of many msbuild version. i.e. if I know my target solution is authored using c# 6.0, I would use msbuild 14.0, else I would like to use msbuild 12.0. Having to include msbuild 14.0 as a dependency would require me to upgrade to .net 4.6.2 I guess? I would rather not impose that on all my users yet.

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.