How to create class libraries with multiple target frameworks
Create a new class library
Use the dotnet cli command
dotnet new classlib -f netstandard2.0
. Note that we explicitely asked to create a library that targets .NET Standard 2.0 but it could have been anything else.Edit the .csproj file
Find:
<TargetFramework>netstandard2.0</TargetFramework>
And replace it with:
<TargetFrameworks>net7.0;net48</TargetFrameworks>
Your library will now be compiled to .NET 7 and .NET Framework 4.8. Here again, you could choose to target other frameworks, or more of them. You can get a complete list of framework identifiers to use in your .csproj here: https://learn.microsoft.com/en-us/dotnet/standard/frameworks#supported-target-frameworks
Ensure that your library is compatible with the frameworks
If you need to provide different implementations based on the target framework, you can use compiler directives. For example:
public void Foo() { #if NET7_0 // Implementation for .NET 7.0 #elif NET48 // Implementation for .NET Framework 4.8 #else #error Unsupported target framework #endif }
You can find a list of compiler directives that can be used based on your target frameworks here: https://learn.microsoft.com/en-us/dotnet/core/tutorials/libraries#preprocessor-symbols
It is possible that you need to reference different NuGet packages (or any other kind of dependency) depending on the target framework. For instance, if you are targeting .NET Standard 2.0 and .NET 7.0 and need to use types from the
System.Reflection.Emit
namespaces, you would need to import a NuGet package when targeting .NET Standard 2.0 because it is not directly supported by .NET Standard 2.0. To do this, use a condition in your .csproj:<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFrameworks>net7.0;netstandard2.0</TargetFrameworks> </PropertyGroup> <ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'"> <PackageReference Include="System.Reflection.Emit" Version="4.7.0" /> </ItemGroup> </Project>