dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.35k stars 4.74k forks source link

[API Proposal]: Add missing IsPlatform methods to `OperatingSystem` class #108632

Open HadrienPatte opened 1 month ago

HadrienPatte commented 1 month ago

Background and motivation

The Proposal is the addition of an IsNetBSD, IsIllumos and IsSolaris methods to the OperatingSystem class.

The OperatingSystem class is already able to identify the following platforms through its IsOSPlatform method.

Platform IsPlatform method available
BROWSER :heavy_check_mark:
WASI :heavy_check_mark:
WINDOWS :heavy_check_mark:
OSX :heavy_check_mark:
MACCATALYST :heavy_check_mark:
IOS :heavy_check_mark:
TVOS :heavy_check_mark:
ANDROID :heavy_check_mark:
LINUX :heavy_check_mark:
FREEBSD :heavy_check_mark:
NETBSD :x:
ILLUMOS :x:
SOLARIS :x:

API Proposal

namespace System
{
    public sealed class OperatingSystem : ISerializable, ICloneable
    {
        /// <summary>
        /// Indicates whether the current application is running on NetBSD.
        /// </summary>
        [NonVersionable]
        public static bool IsNetBSD()

        /// <summary>
        /// Indicates whether the current application is running on Illumos.
        /// </summary>
        [NonVersionable]
        public static bool IsIllumos()

        /// <summary>
        /// Indicates whether the current application is running on Solaris.
        /// </summary>
        [NonVersionable]
        public static bool IsSolaris()
    }
}

API Usage

if (OperatingSystem.IsNetBSD())
{
    Console.WriteLine("Running on NetBSD");
}
else if (OperatingSystem.IsIllumos())
{
    Console.WriteLine("Running on Illumos");
}
else if (OperatingSystem.IsSolaris())
{
    Console.WriteLine("Running on Solaris");
}

Alternative Designs

The proposed feature can already be accomplished with OperatingSystem.IsOSPlatform("NETBSD"), OperatingSystem.IsOSPlatform("ILLUMOS") and OperatingSystem.IsOSPlatform("SOLARIS"), but having an IsPlatform method available for each supported platform would make the overall API of the OperatingSystem class more consistent and predictable.

Risks

No response

teo-tsirpanis commented 1 month ago

We will also need Is***VersionAtLeast overloads.

dotnet-policy-service[bot] commented 1 month ago

Tagging subscribers to this area: @dotnet/area-system-runtime See info in area-owners.md if you want to be subscribed.