Search code examples
.netvisual-studiomercurialproject-managementproject-organization

Targeting multiple frameworks, how to implement?


I have a c# library (developing in VS2010) in Mercurial rep, which currently targets netFX-3.5. There are some features in netFX-4.0 which I want to use in my library, but since there is lot of projects which are limited to 2.0 CLR I also want to keep and maintain netFX-3.5 version. Naturally there will be some code (maybe even most of the code) which will be identical in both versions. Now I'm puzzled that is the best (or at least good) way to maintain this.

Should I create a separate project in VS? Or maybe create a clone of repository? Is there some proven strategies for that?

Update: I've found VS Add as Link feature, which allows a sharing of a file between projects, which is a huge plus towards implementing two targets as a two projects in the same solution. I'm pretty sure I go with this solution, but if there are some more advices, it would be great!

Update2: As I've said earlier, used Add as link feature to achive deired result as follows: create a new project for an old (3.5) framework. Add all files as a link from top version project. In v4.0 project add netFX-4.0 features to a class via partial class specification like ClassName.Net40.cs. Works fine for me.


Solution

  • I don't know about any proven strategy, because it really depends on what will be different and how. Without knowing specifically, I can offer a couple ideas that will probably help.

    #if

    One way is to create separate build configurations for .NET 3.5 and .NET 4.0 so that you can specify separate conditional compilation symbols in each build configuration and do things like this in certain places:

    #if NET40
        // something specific to .NET 4.0
    #else
        // version for .NET 3.5
    #endif
    

    Build configurations make it easy to switch from one to another and to build all of them at once. They're mainly helpful with code, however.

    MQ

    The way that I did this for one project (for a while, I needed a .NET 3.5 version of Caliburn.Micro) was to use Mercurial Queues to have a patch I could apply over the repository to "downgrade" things. Using MQ was a necessary part of the solution because I still needed to be able to update my clone of the CM repository and re-apply my changes.

    Another reason conditional compilation wouldn't help me help here is that CM's reference to the Blend SDK Interactivity assembly needed to change to a previous version, and I didn't know how to do conditional references in the project file.

    (If you haven't used MQ before: MqTutorial, and Chapters 12 & 13 of Mercurial: The Definitive Guide.)