How can I create versions of a class library to support multiple versions of the .NET Framework?

I am trying to create a family of class libraries that are used between multiple applications within an organization. Some of these applications target .NET 3.5 and some target 4.0.

We want to provide two versions of assemblies: one targeting for 3.5 and one targeting 4.0, so that host applications can choose which one is most suitable for them.

There are some subtle differences between versions 3.5 and 4.0:

  • Some classes have been removed due to replacement versions of .NET 4.0.
  • Some classes have been added to help work with classes introduced by .NET 4.0.
  • Some classes have been changed in version 4.0 to take advantage of the advanced threading support in 4.0.

Is there any solution that will allow me to reuse the overlapping parts of the code base and prevent simple formatting of the source tree?

My goal is to create several versions of assemblies oriented to their specific structures, which can be turned into a NuGet package and opened on our internal channel.

+4
source share
3 answers

If you plan to write a family of class libraries that will support multiple .NET frameworks (and / or platforms), you may need to create a "Portable Class Library".

Full details can be found in the blog post Targeting multiple platforms using handheld code (overview) . In Visual Studio 2012, you simply create a new “Portable Class Library” project, while in Visual Studio 2010 you need to install Portable Library Tools first.

With the PCL project, you can target the following platforms:

  • .NET Framework 4, 4.0.3 and 4.5
  • .NET for Metro style apps (which I assume includes Windows Phone 8 and Windows 8 RT)
  • Windows Phone 7.x
  • Silverlight 4 and 5
  • Xbox 360

However, note that the .NET Framework 3.5 is not currently included in this list. Portable Library tools have been written so that in the future other platforms with Mono support can be added at the top of the list.

+1
source

This sounds like a job for conditional compiler directives.

Rewrite your code with

#If NET35 ... #End If 

and

 #If NET40 ... #End If 

From here, you need to add the compilation constants NET35 and NET40 to your project, and I would suggest first creating custom configurations in the configuration manager, such as DebugNET35 , DebugNET40 , ReleaseNET35 , and ReleaseNET40 . After creating these configurations, you can switch to each configuration and go to the advanced compilation options for your project, where you can set custom constants NET35 or NET40 , depending on the current configuration.

You can also set the target structure in this dialog box, but it will install your framework version globally. To configure a custom target structure for each configuration, follow the Pierre Steps .

After that, select the configuration and compile! I used this technique to share the same code base for the Demo and Full versions of the application.

We hope that future versions of Visual Studio will include a wireframe version that automatically detects: https://connect.microsoft.com/VisualStudio/feedback/details/113323/include-framework-version-defines-automatically

Edit: A new .NET Framework blog article discusses writing portable code libraries in Visual Studio 2010 and 2012: http://blogs.msdn.com/b/dotnet/archive/2012/07/06/targeting-multiple-platforms- with-portable-code-overview.aspx and it looks like a cleaner solution.

+6
source

There might be a better way to do this, but I did the following when targeting different platforms. (Xbox vs PC using XNA)

In configurations, you create several * .csprojs with different target platforms. For example, if it was one project, you would have 2 * .csproj files, one for .NET 3.5, and the other for .NET 4.0.

In the configuration manager, you must configure different platforms for each version. * .csproj (s) will actually refer to the same files. You can also set up conventions so code snippets can target .NET 3.5 vs .NET 4.0. In addition, if you need to rewrite the entire file, * .csproj (s) will link to their own files.

+3
source

All Articles