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; } }
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");
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);
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()
{
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";
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();
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
{
/// </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;
}
}
// 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
{
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")));
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")));
{
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")));
{
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")));
{
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")));
{
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")));
{
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")));
{
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));
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());
+ }
}
}
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]
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));
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);
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)
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));
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);
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)