Skip to content

Instantly share code, notes, and snippets.

@jonlabelle
Created October 11, 2020 14:38
Show Gist options
  • Save jonlabelle/2c451aac70b644082404f22852efbcbf to your computer and use it in GitHub Desktop.
Save jonlabelle/2c451aac70b644082404f22852efbcbf to your computer and use it in GitHub Desktop.
How to specify multiple target frameworks in SDK-style projects
<!-- ==========================================================================
How to specify multiple target frameworks in SDK-style projects
- File: MyProject.csproj
- Source: https://docs.microsoft.com/en-us/dotnet/standard/frameworks#how-to-specify-a-target-framework
When you specify multiple target frameworks, you may conditionally reference
assemblies for each target framework. In your code, you can conditionally
compile against those assemblies by using preprocessor symbols with
if-then-else logic.
The following library project targets APIs of .NET Standard (netstandard1.4)
and .NET Framework (net40 and net45). Use the plural TargetFrameworks element
with multiple target frameworks. The Condition attributes include
implementation-specific packages when the library is compiled for the two .NET
Framework TFMs:
=========================================================================== -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard1.4;net40;net45</TargetFrameworks>
</PropertyGroup>
<!-- Conditionally obtain references for the .NET Framework 4.0 target -->
<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
<Reference Include="System.Net" />
</ItemGroup>
<!-- Conditionally obtain references for the .NET Framework 4.5 target -->
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Reference Include="System.Net.Http" />
<Reference Include="System.Threading.Tasks" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment