views:

218

answers:

2

I am maintaining .net 1.1 and .net 3.5 c# code at once. For this purpose I created two csproject files, one for .net 1.1 and another for .net 3.5.

Now, in my source code I am adding new features that are only available in .net 3.5 version, but I also want the code to compile in VS 2003, without the new features. Is there anyway to do a conditional compile based on the compiler version?

In C++ I can do this by checking the value for the macro _MSC _VER, but I am looking for an C# equivalent.

+1  A: 

You can define a different symbols in each CSPROJ file and refer to those in the C# source.

Curt Hagenlocher
A: 

If you can keep the 3.5 specific code in separate files, you could simply split the file allocation between your two .csproj files (or use 2 different build targets in NAnt) - too bad partial classes only came around in 2.0, or that would make it easier to spread the code around...

If you need to mix the code at the file level, the [Conditional()] attribute can filter out entire methods, but I'm not sure if the compiler will still try to process the code in the method. MSDN says the code won't be compiled into IL but parameters will be type checked, but I haven't tried it out. More info here: http://bartdesmet.net/blogs/bart/archive/2006/08/30/4368.aspx and the MSDN link is here: http://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute.aspx

If that's possible, since you've got 2 project files already, you can specify a different define in each one to set the version - no need to look for a macro when you can make it yourself.

jasondoucette