Skip to main content

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.

<!-- ==========================================================================

  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>