Support Multiple Versions of .NET From A Single Class Library (.NET multi-targeting)

.NET implementations come in a few different versions or “flavors” including .NE Framework, .NET Core, and the evolution of .NET Core into .NET 5+. There’s also .NET Standard which is not an implementation but a specification of API that may (or may not) be implemented in the different “flavors”.

If you’re developing a library (whether this be a public/open source library on NuGet or a library for internal use in your company) you may need to support multiple versions of .NET to allow your library to be consumable by as many people as possible.

You could do this by creating multiple class library projects with each one targeting a different version of .NET. Each of these projects would compile to a DLL file that will support the version of .NET chosen for the class library project. You could use shared/linked source code files to have the same source code compiled in each of the different class library projects – this can become a bit messy and cumbersome.

An alternative is to have a single class library project and make use of multi-targeting.

What is Multi-Targeting in .NET?

Multi-targeting is the ability to compile the same source code multiple times, each time compiling for a different version of .NET.

Each target will result in a separate DLL being produced for a class library project for example.

You can then take all the different DLLs and package them up in  a single NuGet package that can then be installed into projects with different .NET versions.

How to Create a Multi-Targeted Class Library in .NET

If you create a class library project in either Visual Studio or using the .NET CLI, the project XML file will contain the following element:<TargetFramework>net6.0</TargetFramework>

This element describes what version of .NET the compiled DLL will support (or “target”) in the preceding example this is .NET 6.0.

The “net6.0” is a target framework moniker (TFM). There are many TFMs that describe the different version of .NET.

You can specify multiple TFMs to enable multi-targeting. To do this you also change to a TargetFrameworks element. For example to target both .NET 6 and .NET Standard 2.0 you would have: <TargetFrameworks>net6.0;netstandard2.0;</TargetFrameworks>

Now when you build the project you will have 2 DLLs – one for .NET 6 and one for .NET standard.

Depending on the targets you add you may also need to add some conditional items to the project file if certain newer features are not supported in older versions such as nullable reference types. Once you have multi-targeting set up you can even compile different portions of code for the different platforms using condition compiler directives to take account of differences/features in the .NET APIs. You will also need to decide how to handle features that are not available in older versions of .NET and perhaps provide a way for consumers of your library to query whether a specific feature is available in a targeted version.To learn how to do all of these things and how to create/version/package your class libraries check out my Class Libraries in C# 10 Pluralsight course. You can start watching with a free trial.

SHARE:

Add comment

Loading