Coding To Support Both .NET Full Framework and Modern .NET
Sometimes when writing .NET libraries we want to support .NET Full Framework, as well as modern .NET. In many cases the functionality is the same, but sometimes it is different.
For the cases where functionality needs to be different there’s two main concerns:
Different code paths
In many cases the code between .NET Full Framework and modern .NET can be the same for both target frameworks. But, for the cases it needs to be different we can use #if
/ #endif
preprocessor directives to specify when code should be used.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using Dependency.For.Both.Target.Frameworks;
#if NETSTANDARD2_0
using Dependency.For.NETSTANDARD2_0.Only;
#endif
#if NET462
using Dependency.For.NET462.Only;
#endif
namespace MyLibrary
{
public class MyClass
{
public void Run()
{
#if NETSTANDARD2_0
Console.WriteLine("netstandard2.0 only");
#endif
#if NET462
Console.WriteLine(".NET Full Framework only");
#endif
Console.WriteLine("Both target frameworks");
}
}
}
You can get as creative with this technique as you’d like, including constructor parameters. You can wrap entire files with #if NET462
/ #endif
even!
Different dependencies (NuGet packages)
Sometimes you may need different NuGet packages to handle a specific target framework only. We can use a Condition
to scope PackageReference
items to specific target frameworks.
See example MyPackage.csproj:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net462</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Something.For.Both.Full.Framework.And.NetStandard" Version="1.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Something.For.NetStandard.Only" Version="1.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
<PackageReference Include="Something.For.Full.Framework.Only" Version="1.0.0" />
</ItemGroup>
</Project>
Summary
Writing code to support .NET Full Framework and modern .NET can bring about some extra work, but in cases where you need or want to support both this can help!