views:

128

answers:

4

My source code needs to support both .NET version 1.1 and 2.0 ... how do I test for the different versions & what is the best way to deal with this situation.

I'm wondering if I should have the two sections of code inline, in separate classes, methods etc. What do you think?

A: 

I would be asking the question of WHY you have to maintain two code bases, I would pick one and go with it if there is any chance of it.

Trying to keep two code bases in sync with the number of changes, and types of changes would be very complex, and a build process to build for either version would be very complex.

Mitchel Sellers
The point is to have one code base that supports being built for either target platform.
minty
+2  A: 

There are a lot of different options here. Where I work we use #if pragmas but it could also be done with separate assemblies for the separate versions.

Ideally you would at least keep the version dependant code in separate partial class files and make the correct version available at compile time. I would enforce this if I could go back in time, our code base now has a whole lot of #if pragmas and sometimes it can be hard to manage. The worst part of the whole #if pragma thing is that Visual Studio just ignores anything that won't compile with the current defines and so it's very easy to check in breaking changes.

NUnit supports both 1.1 and 2.0 and so is a good choice for a test framework. It's not too hard to use something like NAnt to make separate 1.1 and 2.0 builds and then automatically run the NUnit tests.

Rick Minerich
A: 

We had this problem and we ended up with a "compatability layer" where we implemented a single set of interfaces and utility code for .NET v1.1 and v2.0.

Then our installer laid down the right code for the right version. We used NSIS (free!), and they have functions you can call to determine the .NET version.

Jason Cohen
+1  A: 

If you want to do something like this you will need to use preprocessor commands and conditional compilation symbols.

I would use symbols that clearly indicate the version of .NET you are targeting (say NET11 and NET20) and then wrap the relevant code like this:

#if NET11
// .NET 1.1 code
#elif NET20
// .NET 2.0 code
#endif

The reason for doing it this way rather than a simple if/else is an extra layer of protection in case someone forgets to define the symbol.

That being said, you should really drill down to the heart of the reason why you want/need to do this.

Scott Dorman