What's the best way to target multiple versions of the .NET framework?

后端 未结 3 963
谎友^
谎友^ 2021-01-30 16:24

I\'m building a class library and I will deploy it a NuGet package, which lets me choose different assemblies to be added as references based on the .NET framework version of th

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-30 17:10

    You will at least need one VisualStudio Solution with 2 projects (one for .net 4 and one for .net 4.5).

    Add all codefiles to the .net 4-project and in the other project you add the code files as link (use "Add Existing Item..."-Dialog and chose Add as link)

    Now you add all codes and classes for .NET 4.5 to your 4.5-project.

    Additionally you should define your own compiler switches (conditional compilation symbols) to your projects. Like NET4 for your .net 4-project and NET4.5 to your .net 4.5-project)

    You set the switches in the project settings under Build->General->Conditional Compilation Switches

    In your code you can use the switches as follows to generate code for .NET 4 or .NET 4.5

    #if NET4
      // code only for .NET 4
    #endif
    
    // code for all framework versions.
    
    #if NET45
      // code only for .NET 4.5
    #endif
    

提交回复
热议问题