2

I have a class library which is currently multi-targeting NET40 and NETSTANDARD2.0:

<TargetFrameworks>net40;netstandard2.0</TargetFrameworks>

However, I now need to also support some new api which was added as part of NETCOREAPP2.1 which is not covered by netstandard.

My initial thought is to simply expand the current frameworks to include NETCOREAPP2.1:

<TargetFrameworks>net40;netstandard2.0;netcoreapp2.1</TargetFrameworks>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1'">
    <DefineConstants>NETCORE</DefineConstants> 
</PropertyGroup>

and in the code I can use the newly added API like so:

#if NETCORE
    // Use the api added in NETCORE 2.1
#endif

However what will happen once the library is used in the app targeting later versions of .NET Core? e.g. .NET Core 2.2? Do I have to create new constants for every newly released version?

In an ideal world NETCOREAPP2.1 would implement NETSTANDARD2.1 but unfortunately THIS is not the case.

MaYaN
  • 6,683
  • 12
  • 57
  • 109
  • What is the new api you're looking to use? the .net standard 2.1 release should have brought it into parity with .net core 2.1 – Gibbon Jan 07 '19 at 13:46
  • That's not the case and will never be. Please have a look at the link I included. – MaYaN Jan 07 '19 at 13:48
  • Are you aware of the compiler constants that are already available to you? https://stackoverflow.com/questions/42754123/conditional-per-targetframework-in-msbuild-15/42754213#42754213 – DavidG Jan 07 '19 at 13:57
  • @DavidG Yes but how does that solve this issue? – MaYaN Jan 07 '19 at 13:59
  • 1
    It doesn't, it just saves you from having to do the `PropertyGroup`, otherwise I would have added an answer. – DavidG Jan 07 '19 at 14:00
  • Multitargeting should only be used for the simplest cases. Complex cases should use runtime detection, such as .NET Core version APIs, https://devblogs.microsoft.com/dotnet/announcing-net-core-3-preview-4/ – Lex Li Apr 22 '19 at 18:31

2 Answers2

0

just create a .net standard 2.0 library. this lib can be used by .net (>= 4.6.1) or core (>= 2.0) applications. the downwards compatibility of an lib is guaranteed.

https://learn.microsoft.com/en-us/dotnet/standard/net-standard#net-platforms-support

user1519979
  • 1,854
  • 15
  • 26
  • That's what the current version is doing but the new api is only available on `NETCOREAPP2.1` which is lacking in `NETSTANDARD2.0` – MaYaN Jan 07 '19 at 13:49
  • oh yes, i think it's not possible to use these apis. for using the new apis you may have to use .net core 3 and .net 4.8 which includes .net standard 2.1. here you can check which apis of core 2.1 aren't included in standard 2.1: https://github.com/dotnet/standard/blob/master/docs/planning/netstandard-2.1/README.md – user1519979 Jan 07 '19 at 14:09
  • i think for .net core 3 there's currently just a preview avalaible. else you have to find an 3rd party lib which gives you the functionality of that needed api or you have to code it on your own – user1519979 Jan 07 '19 at 14:13
0

You can use .Net string methods inside your condition:

<PropertyGroup Condition="$(TargetFramework.StartsWith('netcore'))">
    <DefineConstants>NETCORE</DefineConstants> 
</PropertyGroup>
Chris F Carroll
  • 11,146
  • 3
  • 53
  • 61