Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public enum Architecture
public static System.Runtime.InteropServices.OSPlatform FreeBSD { get { throw null; } }
public static System.Runtime.InteropServices.OSPlatform iOS { get { throw null; } }
public static System.Runtime.InteropServices.OSPlatform Linux { get { throw null; } }
public static System.Runtime.InteropServices.OSPlatform macOS { get { throw null; } }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static System.Runtime.InteropServices.OSPlatform OSX { get { throw null; } }
public static System.Runtime.InteropServices.OSPlatform tvOS { get { throw null; } }
public static System.Runtime.InteropServices.OSPlatform watchOS { get { throw null; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ namespace System.Runtime.InteropServices

public static OSPlatform Linux { get; } = new OSPlatform("LINUX");

public static OSPlatform macOS { get; } = new OSPlatform("MACOS");

[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] // superseded by macOS
public static OSPlatform OSX { get; } = new OSPlatform("OSX");

public static OSPlatform iOS { get; } = new OSPlatform("IOS");
Expand All @@ -25,14 +28,21 @@ namespace System.Runtime.InteropServices

public static OSPlatform Windows { get; } = new OSPlatform("WINDOWS");

internal bool IsCurrent { get; } // this information is cached because it's frequently used

private OSPlatform(string osPlatform)
{
if (osPlatform == null) throw new ArgumentNullException(nameof(osPlatform));
if (osPlatform.Length == 0) throw new ArgumentException(SR.Argument_EmptyValue, nameof(osPlatform));

_osPlatform = osPlatform;
IsCurrent = RuntimeInformation.IsCurrentOSPlatform(osPlatform);
}

/// <summary>
/// Creates a new OSPlatform instance.
/// </summary>
/// <remarks>If you plan to call this method frequently, please consider caching its result.</remarks>
public static OSPlatform Create(string osPlatform)
{
return new OSPlatform(osPlatform);
Expand All @@ -45,17 +55,17 @@ public bool Equals(OSPlatform other)

internal bool Equals(string? other)
{
return string.Equals(_osPlatform, other, StringComparison.Ordinal);
return string.Equals(_osPlatform, other, StringComparison.OrdinalIgnoreCase);
}

public override bool Equals(object? obj)
{
return obj is OSPlatform && Equals((OSPlatform)obj);
return obj is OSPlatform osPlatform && Equals(osPlatform);
}

public override int GetHashCode()
{
return _osPlatform == null ? 0 : _osPlatform.GetHashCode();
return _osPlatform == null ? 0 : _osPlatform.GetHashCode(StringComparison.OrdinalIgnoreCase);
}

public override string ToString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace System.Runtime.InteropServices
{
public static partial class RuntimeInformation
{
public static bool IsOSPlatform(OSPlatform osPlatform) => osPlatform.Equals(OSPlatform.Browser);
internal static bool IsCurrentOSPlatform(string osPlatform) => osPlatform.Equals("BROWSER", StringComparison.OrdinalIgnoreCase);

public static string OSDescription => "Browser";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ public static partial class RuntimeInformation
private static Architecture? s_osArch;
private static Architecture? s_processArch;

public static bool IsOSPlatform(OSPlatform osPlatform)
internal static bool IsCurrentOSPlatform(string osPlatform)
{
string name = s_osPlatformName ??= Interop.Sys.GetUnixName();
return osPlatform.Equals(name);

return osPlatform.Equals(name, StringComparison.OrdinalIgnoreCase)
|| (name == "OSX" && osPlatform.Equals("MACOS", StringComparison.OrdinalIgnoreCase)); // GetUnixName returns OSX on macOS
}

public static string OSDescription => s_osDescription ??= Interop.Sys.GetUnixVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ public static partial class RuntimeInformation
private static Architecture? s_osArch;
private static Architecture? s_processArch;

public static bool IsOSPlatform(OSPlatform osPlatform)
{
return OSPlatform.Windows == osPlatform;
}
internal static bool IsCurrentOSPlatform(string osPlatform) => osPlatform.Equals("WINDOWS", StringComparison.OrdinalIgnoreCase);

public static string OSDescription
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@ public static string FrameworkDescription
/// </remarks>
public static string RuntimeIdentifier =>
s_runtimeIdentifier ??= AppContext.GetData("RUNTIME_IDENTIFIER") as string ?? "unknown";

/// <summary>
/// Indicates whether the current application is running on the specified platform.
/// </summary>
public static bool IsOSPlatform(OSPlatform osPlatform) => osPlatform.IsCurrent;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;
using Xunit;

namespace System.Runtime.InteropServices.RuntimeInformationTests
Expand All @@ -13,10 +12,10 @@ public void CheckLinux()
{
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Linux));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("LINUX")));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("linux")));

Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("DARWIN")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("linux")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NetBSD")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("netbsd")));
Expand All @@ -31,9 +30,9 @@ public void CheckLinux()
public void CheckNetBSD()
{
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD")));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NetBSD")));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("netbsd")));

Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NetBSD")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("netbsd")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("DARWIN")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("LINUX")));
Expand All @@ -51,12 +50,16 @@ public void CheckOSX()
{
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.OSX));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("OSX")));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("osx")));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.macOS));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("MACOS")));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("macOS")));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("macos")));

Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NetBSD")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("netbsd")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("osx")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("mac")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("DARWIN")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("MACOSX")));
Expand All @@ -70,6 +73,8 @@ public void CheckiOS()
{
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.iOS));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("IOS")));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("iOS")));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("ios")));

Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD")));
Expand All @@ -89,6 +94,8 @@ public void ChecktvOS()
{
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.tvOS));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("TVOS")));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("tvOS")));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("tvos")));

Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD")));
Expand All @@ -108,6 +115,7 @@ public void CheckAndroid()
{
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Android));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("ANDROID")));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("android")));

Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD")));
Expand All @@ -127,6 +135,7 @@ public void CheckBrowser()
{
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Browser));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER")));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("browser")));

Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD")));
Expand All @@ -146,13 +155,14 @@ public void CheckWindows()
{
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("WINDOWS")));
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("windows")));

Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NetBSD")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("netbsd")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("windows")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("Windows NT")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("win")));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Linux));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.OSX));
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD));
Expand Down Expand Up @@ -213,5 +223,17 @@ public void CheckOSPlatform()
Assert.Equal(0, defaultObj.GetHashCode());
Assert.Equal(defaultObj.GetHashCode(), conObj.GetHashCode());
}

[Fact]
public void StringComparisonOrdinalIgnoreCaseIsUsed()
{
Assert.Equal(OSPlatform.Create("A"), OSPlatform.Create("a"));
Assert.Equal(OSPlatform.Create("A"), OSPlatform.Create("A"));
Assert.Equal(OSPlatform.Create("a"), OSPlatform.Create("a"));

Assert.Equal(OSPlatform.Create("A").GetHashCode(), OSPlatform.Create("a").GetHashCode());
Assert.Equal(OSPlatform.Create("A").GetHashCode(), OSPlatform.Create("A").GetHashCode());
Assert.Equal(OSPlatform.Create("a").GetHashCode(), OSPlatform.Create("a").GetHashCode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public static IEnumerable<object[]> AllKnownOsPlatforms()
yield return new object[] { OSPlatform.Linux };
yield return new object[] { OSPlatform.OSX };
yield return new object[] { OSPlatform.Browser };
yield return new object[] { OSPlatform.macOS };
yield return new object[] { OSPlatform.iOS };
yield return new object[] { OSPlatform.tvOS };
yield return new object[] { OSPlatform.watchOS };
yield return new object[] { OSPlatform.Android };
}

[Fact]
Expand Down Expand Up @@ -49,6 +54,8 @@ public void IsOSPlatformOrLater_ReturnsTrue_ForCurrentOS(OSPlatform osPlatform)
Version current = Environment.OSVersion.Version;

Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater($"{osPlatform}{current}"));
Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater($"{osPlatform.ToString().ToLower()}{current}"));
Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater($"{osPlatform.ToString().ToUpper()}{current}"));

Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater(osPlatform, current.Major));

Expand Down Expand Up @@ -78,6 +85,8 @@ public void IsOSPlatformOrLater_ReturnsFalse_ForNewerVersionOfCurrentOS(OSPlatfo

Version newer = new Version(currentVersion.Major + 1, 0);
Assert.False(RuntimeInformation.IsOSPlatformOrLater($"{osPlatform}{newer}"));
Assert.False(RuntimeInformation.IsOSPlatformOrLater($"{osPlatform.ToString().ToLower()}{newer}"));
Assert.False(RuntimeInformation.IsOSPlatformOrLater($"{osPlatform.ToString().ToUpper()}{newer}"));
Assert.False(RuntimeInformation.IsOSPlatformOrLater(osPlatform, newer.Major));

newer = new Version(currentVersion.Major, currentVersion.Minor + 1);
Expand All @@ -104,6 +113,8 @@ public void IsOSPlatformOrLater_ReturnsTrue_ForOlderVersionOfCurrentOS(OSPlatfor

Version older = new Version(current.Major - 1, 0);
Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater($"{osPlatform}{older}"));
Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater($"{osPlatform.ToString().ToLower()}{older}"));
Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater($"{osPlatform.ToString().ToUpper()}{older}"));
Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater(osPlatform, older.Major));

if (current.Minor > 0)
Expand Down Expand Up @@ -160,6 +171,8 @@ public void IsOSPlatformEarlierThan_ReturnsFalse_ForCurrentOS(OSPlatform osPlatf
Version current = Environment.OSVersion.Version;

Assert.False(RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform}{current}"));
Assert.False(RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform.ToString().ToLower()}{current}"));
Assert.False(RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform.ToString().ToUpper()}{current}"));

Assert.False(RuntimeInformation.IsOSPlatformEarlierThan(osPlatform, current.Major));

Expand Down Expand Up @@ -190,6 +203,8 @@ public void IsOSPlatformEarlierThan_ReturnsTrue_ForNewerVersionOfCurrentOS(OSPla

Version newer = new Version(current.Major + 1, 0);
Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform}{newer}"));
Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform.ToString().ToLower()}{newer}"));
Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform.ToString().ToUpper()}{newer}"));
Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformEarlierThan(osPlatform, newer.Major));

newer = new Version(current.Major, current.Minor + 1);
Expand All @@ -215,6 +230,8 @@ public void IsOSPlatformEarlierThan_ReturnsFalse_ForOlderVersionOfCurrentOS(OSPl

Version older = new Version(current.Major - 1, 0);
Assert.False(RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform}{older}"));
Assert.False(RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform.ToString().ToLower()}{older}"));
Assert.False(RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform.ToString().ToUpper()}{older}"));
Assert.False(RuntimeInformation.IsOSPlatformEarlierThan(osPlatform, older.Major));

if (current.Minor > 0)
Expand Down