Paul Turner
Paul Turner

Reputation: 39625

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

I'm trying to author a family of class libraries which are used between multiple applications within the organization. Some of those applications target .NET 3.5 and some target 4.0.

We want to provide two versions of the assemblies, one targeting 3.5 and one targeting 4.0, so that the host applications can select whichever assembly is most appropriate for them.

There are some subtle differences between the 3.5 and 4.0 versions:

Is there any solution that will allow me to re-use the overlapping portions of the code-base and prevent simply forking the source tree?

My objective is to produce multiple versions of the assemblies targeting their specified frameworks which can be rolled into a NuGet package and exposed on our internal feed.

Upvotes: 4

Views: 1383

Answers (3)

MCattle
MCattle

Reputation: 3167

If you're planning on writing a family of class libraries that will support multiple .NET frameworks (and/or platforms), you may want to create a "Portable Class Library".

Full details are in the blog post Targeting Multiple Platforms with Portable Code (Overview). In Visual Studio 2012, you simply create a new "Portable Class Library" project, whereas in Visual Studio 2010 you'll need to install the Portable Library Tools first.

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

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

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

Upvotes: 1

MCattle
MCattle

Reputation: 3167

This sounds like a job for conditional compiler directives.

Pepper your code with:

#If NET35
...
#End If

and

#If NET40
...
#End If

From here, you have to add the NET35 and NET40 compilation constants to your project, and I would suggest first creating custom configurations in the configuration manager, such as DebugNET35, DebugNET40, ReleaseNET35, and ReleaseNET40. Once those configurations are created, you can switch to each configuration and go to the Advanced Compile Options for your project, where you can set the custom constants NET35 or NET40, depending on the current configuration.

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

After that, pick a configuration and compile! I've used this technique to share the same code base for "Demo" and "Full" versions of an application.

Hopefully, future versions of Visual Studio will include framework version defines automatically: https://connect.microsoft.com/VisualStudio/feedback/details/113323/include-framework-version-defines-automatically

Edit: There's a new article in the .NET Framework Blog that 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 this looks like a much cleaner solution.

Upvotes: 6

Tim Sullivan
Tim Sullivan

Reputation: 94

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

You create multiple *.csproj with different platform targets in the configurations. For example if it was a single project, you would have 2 *.csproj files, one for .NET 3.5 and the other for .NET 4.0.

In the configruation manager you would set up different platforms for each release. The *.csproj(s) would actually be referencing the same files. You could also set up build conditional symbols so that pieces of code could be targeted for .NET 3.5 vs .NET 4.0. Additionally if you need to re-write an entire file, the *.csproj(s) would reference thier own files.

Upvotes: 3

Related Questions