dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.68k stars 1.06k forks source link

Define NET5_0_WINDOWS when TargetFramework is net5.0-windows #20825

Open SteveBush opened 3 years ago

SteveBush commented 3 years ago

I'm building a multi-platform library that includes System.Managment API support in the windows version of the library. I've been using a TargetFrameworks net6.0-windows and net5.0-windows to indicate the inclusion of the Windows-specific code. The net6.0, net6.0-XXXX, and net5.0 versions of the library include code that handles unsupported platforms.

Below is the list of frameworks, I'm targeting.

<TargetFrameworks>netstandard2.1;netstandard2.0;net6.0;net6.0-windows;net5.0-windows;net6.0-android;net6.0-ios;net6.0-maccatalyst;net6.0-macos;</TargetFrameworks>

All of this works great except that I cannot distinguish between net5.0 and net5.0-windows targetframeworks using compiler defines. Ideally, NET5_0_WINDOWS could be defined similarly to what happens with NET6_0_WINDOWS.

Describe the solution you'd like

In the code below Abstractions.Extensions.AssemblyExtensions.FrameworkName returns the FrameworkName from the current Executing Assembly.

        /// <summary>
        /// Gets the target framework first attribute.
        /// </summary>
        /// <value>The target framework attribute.</value>
        public static TargetFrameworkAttribute? TargetFrameworkAttribute =>
            (TargetFrameworkAttribute?)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(TargetFrameworkAttribute), false).FirstOrDefault();

           /// <summary>
        /// Gets the name of the framework.
        /// </summary>
        /// <value>The name of the framework.</value>
        public static string? FrameworkName => TargetFrameworkAttribute?.FrameworkName;

I have created a set of xUnit unit tests to test the method above on various frameworks. As you can see from the code below, I need to distinguish between Net5.0 and Net5.0-Windows.

As you can see below, the NETCOREAPPX_1 tests are picking up the code from the .NetStandard libraries. All .net6.0 tests are picking up the NETCoreApp,Version=v6.0 library.

The net5.0-windows version is returning .NETCoreApp,Version=v5.0 and the .net5.0 version is returning the .NETStandard,Version=v2.1 library.

The request is to define NET5_0_WINDOWS in a similar manner to NET6_0_WINDOWS. This would allow me to distinguish between the NETCoreApp (net5.0-windows) and NETStandard (net5.0) versions of the library.

I have already handled this in my build environment by adding:

  <PropertyGroup Condition=" $(TargetFramework.StartsWith('net5.0-windows')) ">
    <DefineConstants>$(DefineConstants);NET5_0_WINDOWS</DefineConstants>
  </PropertyGroup>
#if NET5_0_WINDOWS
            Abstractions.Extensions.AssemblyExtensions.FrameworkName.Should().Be(".NETCoreApp,Version=v5.0");
#elif NET5_0
            Abstractions.Extensions.AssemblyExtensions.FrameworkName.Should().Be(".NETStandard,Version=v2.1");
#elif NET6_0
            Abstractions.Extensions.AssemblyExtensions.FrameworkName.Should().Be(".NETCoreApp,Version=v6.0");
#elif NETCOREAPP2_1
            Abstractions.Extensions.AssemblyExtensions.FrameworkName.Should().Be(".NETStandard,Version=v2.0");
#elif NETCOREAPP3_1
            Abstractions.Extensions.AssemblyExtensions.FrameworkName.Should().Be(".NETStandard,Version=v2.1");
#elif NET461
            Abstractions.Extensions.AssemblyExtensions.FrameworkName.Should().Be(".NETFramework,Version=v4.6.1");
#else
            Assert.Fail($"Unknown FrameworkName: {Abstractions.Extensions.AssemblyExtensions.FrameworkName}");
#endif
reflectronic commented 3 years ago

I believe

#if NET5_0 && WINDOWS

should work.

dsplaisted commented 3 years ago

I believe

#if NET5_0 && WINDOWS

should work.

Yes, this is probably what you should do.

For reference, here are the relevant designs:

https://github.com/dotnet/designs/blob/main/accepted/2020/or-greater-defines/or-greater-defines.md https://github.com/dotnet/designs/blob/main/accepted/2020/net5/net5.md#preprocessor-symbols