R – How to get NAnt to use the new VB9 compiler for .NET 2.0 applications

.net-2.0compiler-constructionnantvb.netvisual-studio-2008

How do I tell NAnt to use the same VB compiler VS2008 uses when it creates .NET 2.0-targeting applications?

I have switched a web application to VS2008 back-targetted to .NET 2.0. I can run NAnt (0.85rc4, 0.85, and 0.86b1) just fine once I do this. When I try to use some of the VB9 syntax, that still compiles just fine back to a .NET 2.0 binary in VS2008, NAnt gets the kind of compile error you would get if you tried to do the new syntax in VS2005 (where it wasn't supported).

In case it helps, here is a simplified version of what I am attempting, just a simple anonymous delegate that works great until I try to use NAnt to build the 2.0 app instead of VS2008.

Public Class SomeObject
    Public Name As String
End Class
Private SomeList As List(Of SomeObject) = SomeObject.GetAllSomeObjects()
Public Function FindFirstItemWithSimilarName(ByVal x As String) As SomeObject
    Return SomeList.Find(Function(p As SomeObject) p.Name.Contains(x))
End Function

EDIT: Unless someone can think of a reason not to, the current setting in my build file is this (since I do indeed want a .NET 2.0 application, just one generated by a more robust VB compiler):

<property name="nant.settings.currentframework" value="net-2.0"/>

Best Answer

I wonder if you have to change the framework that NAnt is running under.

Also, look into the NAntContrib project. That has the MSBuild task. We use this to build our 2008 projects, where we use the fancy sytax sugar like what you want, down to 2.0 assemblies. One thing we had to do though was replace the MSBuild.exe in the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 with one that forwards the request on to the 3.0 MSBuild.exe

Here is the code we use to replace MSBuild.exe in the 2.0 path. I can't remember where I got this code. I tried to find it online but can't.

Build this to MSBuild.exe and replace your 2.0 one with this one.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace MSBuild
{
    public class Program
    {
        public static int Main(string[] args)
        {
            for (int argIndex = 0; argIndex < args.Length; argIndex++)
            {
                if (args[argIndex].Contains(" "))
                {
                    string quotedArg = string.Format("\"{0}\"", args[argIndex]);
                    args[argIndex] = quotedArg;
                }
            }

            string arguments = string.Join(" ", args);

            // HACK: I should probably change
            //       this to automaticlaly 
            //       determine the path

            Process process = Process.Start("C:\\WINDOWS\\Microsoft.NET\\Framework\\v3.5\\MSBuild.exe",arguments);

            process.WaitForExit();

            return process.ExitCode;

        }
    }
}