Skip to content

Consider switching from RuntimeInformation.IsOSPlatform(XYZ) to OperatingSystem.IsXYZ() #24653

@adamsitnik

Description

@adamsitnik

In dotnet/runtime#40457 new methods that allow for instant OS checks were introduced:

namespace System
{
    public partial class OperatingSystem
    {
        // Primary
        public static bool IsOSPlatform(string platform);
        public static bool IsOSPlatformVersionAtLeast(string platform, int major, int minor = 0, int build = 0, int revision = 0);

        // Accelerators for platforms where versions don't make sense
        public static bool IsBrowser();
        public static bool IsLinux();

        // Accelerators with version checks

        public static bool IsFreeBSD();
        public static bool IsFreeBSDVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0);

        public static bool IsAndroid();
        public static bool IsAndroidVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0);

        public static bool IsIOS();
        public static bool IsIOSVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0);

        public static bool IsMacOS();
        public static bool IsMacOSVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0);

        public static bool IsTvOS();
        public static bool IsTvOSVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0);

        public static bool IsWatchOS();
        public static bool IsWatchOSVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0);

        public static bool IsWindows();
        public static bool IsWindowsVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0);
    }
}

All the IsXYZ methods (not IsXYZVersionAtLeast()) are basically returning constants and thanks to that, JIT is capable of performing dead code elimination:

if (OperatingSystem.IsWindows())
{
    // gets removed as dead code when running on non-windows OSes
}

For net5.0 libs/apps you might consider switching from the old API (RuntimeInformation.IsOSPlatform(XYZ)) to the new one.

/cc @davidfowl

Metadata

Metadata

Assignees

No one assigned

    Labels

    affected-allThis issue impacts all the customersarea-blazorIncludes: Blazor, Razor Componentsarea-infrastructureIncludes: MSBuild projects/targets, build scripts, CI, Installers and shared frameworkfeature-blazor-wasmThis issue is related to and / or impacts Blazor WebAssemblyhelp wantedUp for grabs. We would accept a PR to help resolve this issueseverity-nice-to-haveThis label is used by an internal tooltask

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions