tags:

views:

546

answers:

1

How do you specify a target framework version for the csc.exe c# compiler via command-line invocation (e.g., no .csproj file and not going thru the MSBUILD engine)?

e.g, using the C# 3.0 csc.exe compiler, how do you compile to IL targeting the 2.0 .net framework?

+2  A: 

In the specific case of the C# 3 compiler, there isn't a problem so long as you don't use any assemblies or types which aren't in .NET 2.0 - the IL is the same (as opposed to targeting 1.1, for instance).

In addition to this, you can use /noconfig /nostdlib and then explicitly reference the .NET 2.0 assemblies (in c:\Windows\Microsoft.NET\Framework\v2.0.50727 for example). It looks like the /lib command line option can make this slightly easier by letting you specify a directory to look in for references, but I haven't tried that myself.

Jon Skeet
This would seem to work (explicitly referencing the 2.0 libs) but the first suggestion to just rely on the fact that the IL is the same) would seem to leave me 'exposed' (e.g., no compile-time warning that I had strayed from the 2.0-only assemblies).
sbohlen
Yes, it would leave you exposed to that. Be careful about the differences between .NET 2.0 and 2.0SP1 as well (e.g. DateTimeOffset) - I don't know whether you'll easily be able to find the "vanilla" 2.0 assemblies to reference. Of course, you could copy them from a non-SP1 machine...
Jon Skeet
Yeah, I think that as far as I can tell, this is something that I will have to look into re: how its 'enforced' when MSBUILD is fired off against the .csproj file (where its possible of course to spec the target FW version). Seems no bulletproof csc.exe option is available to me.
sbohlen