0

Supose that I have a multi target project, something like that:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <PackageId>Bugsnag</PackageId>
    <Title>Bugsnag .NET Notifier</Title>
    <TargetFrameworks>net35;net40;net45;netstandard1.3;netstandard2.0</TargetFrameworks>
  </PropertyGroup>
</Project>

I am targeting to net35, net40, net45 and others. Net40 has features that net35 has not, and net45 has features that net40 has not.

So my doubt is, when I code my application, do I have available all the features of net45? And if it is true, how is it possible can it compile if for example, net35 has not some of the features that I am using?

Thanks so much.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193

1 Answers1

2

When you target multiple frameworks you must use conditional references and preprocessor symbols to work with features that are not supported by all of the target frameworks.

Let's say that you target .NET 4.0 and 4.5:

<PropertyGroup>
  <TargetFramework>net40;net45</TargetFramework>
</PropertyGroup>

If you have an optional dependency that uses framework 4.5 but not 4.0 then you can conditionally reference it in your projects:

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
  <Reference Include="Referenced.Assembly.Name" />
</ItemGroup>

You may also need to use this to reference different versions of a dependency for each framework.

In your code you can use preprocessor symbols to ensure that newer framework features do not throw errors when building against an older framework:

public void Example()
{
#if NET45
    // Implementation that uses the 4.5 framework
#else
    // Implementation that uses the 4.0 framework
#endif
}

The full documentation for multi-targeting frameworks can be found here:
Target Frameworks | Microsoft Docs

Romen
  • 1,617
  • 10
  • 26