diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/ref/System.Runtime.InteropServices.RuntimeInformation.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/ref/System.Runtime.InteropServices.RuntimeInformation.cs
index e9c675b61f2e89..46d2dd5159e22f 100644
--- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/ref/System.Runtime.InteropServices.RuntimeInformation.cs
+++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/ref/System.Runtime.InteropServices.RuntimeInformation.cs
@@ -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; } }
diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/OSPlatform.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/OSPlatform.cs
index 39f956e164bd03..18e45643d216f4 100644
--- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/OSPlatform.cs
+++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/OSPlatform.cs
@@ -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");
@@ -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);
}
+ ///
+ /// Creates a new OSPlatform instance.
+ ///
+ /// If you plan to call this method frequently, please consider caching its result.
public static OSPlatform Create(string osPlatform)
{
return new OSPlatform(osPlatform);
@@ -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()
diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Browser.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Browser.cs
index 769d4ae38f30f9..838aa49c2e76ec 100644
--- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Browser.cs
+++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Browser.cs
@@ -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";
diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Unix.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Unix.cs
index c930aac9254063..ea7db57e016f81 100644
--- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Unix.cs
+++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Unix.cs
@@ -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();
diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Windows.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Windows.cs
index 0ccc9deb930047..7c71cc972aad5d 100644
--- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Windows.cs
+++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Windows.cs
@@ -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
{
diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs
index 8e43d13ec46389..95b1b62b4cdbe7 100644
--- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs
+++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs
@@ -48,5 +48,10 @@ public static string FrameworkDescription
///
public static string RuntimeIdentifier =>
s_runtimeIdentifier ??= AppContext.GetData("RUNTIME_IDENTIFIER") as string ?? "unknown";
+
+ ///
+ /// Indicates whether the current application is running on the specified platform.
+ ///
+ public static bool IsOSPlatform(OSPlatform osPlatform) => osPlatform.IsCurrent;
}
}
diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs
index 1264c5c0d850ad..e40f9f8b310afe 100644
--- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs
+++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs
@@ -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
@@ -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")));
@@ -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")));
@@ -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")));
@@ -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")));
@@ -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")));
@@ -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")));
@@ -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")));
@@ -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));
@@ -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());
+ }
}
}
diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/PlatformVersionTests.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/PlatformVersionTests.cs
index c00ace31490338..b612d2e065bcba 100644
--- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/PlatformVersionTests.cs
+++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/PlatformVersionTests.cs
@@ -14,6 +14,11 @@ public static IEnumerable