C# – With NAnt, How to use C# 3.0 compiler while targetting .NET 2.0 runtime

.net-2.0cc#-3.0nantnet

I'm using NAnt 0.85 to build a legacy project. The script itself uses the csc task (not the msbuild task) and works fine.

The project is on its way to migrating over .NET 3.5. We already use VS2008, and C# 3.0, while still targeting .NET 2.0 framework runtime.

Now the problem occurs when we want to upgrade our NAnt scripts, to compile C#3.0 code using csc task.

I managed to compile C#3.0 code with NAnt, by modifying the nant.exe.config to add the net-3.5 framework section, but still, I can't figure a way to ensure that resulting assemblies will execute on .NET 2.0 runtime.

More problematic : if I'm using LinqBridge to leverage Linq without System.Core and .NET 3.5 runtime, the csc task fails and gave me an error message indicating that all linq-related calls are ambiguous. In fact, NAnt csc task seems to automatically reference System.Core.dll, which causes a conflict with the only reference I effectively added to the <references> section, under the csc task: LinqBridge.dll.

Do you now how to solve that?

Best Answer

Executing on .NET 2.0 is mostly just a case of making sure you don't add a reference to any .NET 3.5-specific libraries. There are other subtleties where new types (e.g. DateTimeOffset) have been added to .NET 2.0 SP1 in existing libraries, but that's a different matter.

I suspect the LinqBridge problem is due to csc using the default response file. It's possible that specifying

noconfig="true"

will fix this. The documentation states that noconfig "Instructs the compiler not to use implicit references to assemblies" which sounds like what you want.