-
Notifications
You must be signed in to change notification settings - Fork 5.3k
add IsOSPlatformOrLater and IsOSPlatformEarlierThan methods #39005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f11bbdc
add tests for IsOSPlatformOrLater and IsOSPlatformEarlierThan methods
adamsitnik cb65eca
add IsOSPlatformOrLater and IsOSPlatformEarlierThan methods
adamsitnik d812250
add xml docs that explain what the API does and what input is supported
adamsitnik 14b233c
address code review feedback
adamsitnik 9668641
numbers in the middle of the platform name are not supported
adamsitnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
...c/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.PlatformVersion.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System.Runtime.InteropServices | ||
| { | ||
| public static partial class RuntimeInformation | ||
| { | ||
| /// <summary> | ||
| /// Check for the OS with a >= version comparison. Used to guard APIs that were added in the given OS release. | ||
| /// </summary> | ||
| /// <param name="platformName">OS name concatenated with a version number.</param> | ||
| /// <remarks>The version number must contain at least major and minor numbers separated with a dot. | ||
| /// Example: "ios14.0" is OK, "ios14" is NOT OK.</remarks> | ||
| public static bool IsOSPlatformOrLater(string platformName) | ||
| { | ||
| (OSPlatform platform, Version version) = Parse(platformName); | ||
|
|
||
| return IsOSPlatformOrLater(platform, version.Major, version.Minor, version.Build, version.Revision); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Check for the OS with a >= version comparison. Used to guard APIs that were added in the given OS release. | ||
| /// </summary> | ||
| public static bool IsOSPlatformOrLater(OSPlatform osPlatform, int major) | ||
| => IsOSPlatform(osPlatform) && Environment.OSVersion.Version.Major >= major; | ||
|
|
||
| /// <summary> | ||
| /// Check for the OS with a >= version comparison. Used to guard APIs that were added in the given OS release. | ||
| /// </summary> | ||
| public static bool IsOSPlatformOrLater(OSPlatform osPlatform, int major, int minor) | ||
| => IsOSPlatform(osPlatform) && IsOSVersionOrLater(major, minor, int.MinValue, int.MinValue); | ||
|
|
||
| /// <summary> | ||
| /// Check for the OS with a >= version comparison. Used to guard APIs that were added in the given OS release. | ||
| /// </summary> | ||
| public static bool IsOSPlatformOrLater(OSPlatform osPlatform, int major, int minor, int build) | ||
| => IsOSPlatform(osPlatform) && IsOSVersionOrLater(major, minor, build, int.MinValue); | ||
|
|
||
| /// <summary> | ||
| /// Check for the OS with a >= version comparison. Used to guard APIs that were added in the given OS release. | ||
| /// </summary> | ||
| public static bool IsOSPlatformOrLater(OSPlatform osPlatform, int major, int minor, int build, int revision) | ||
| => IsOSPlatform(osPlatform) && IsOSVersionOrLater(major, minor, build, revision); | ||
|
|
||
| /// <summary> | ||
| /// Check for the OS with a < version comparison. Used to guard APIs that were obsoleted or removed in the given OS release. | ||
| /// </summary> | ||
| /// <param name="platformName">OS name concatenated with a version number.</param> | ||
| /// <remarks>The version number must contain at least major and minor numbers separated with a dot. | ||
| /// Example: "ios14.0" is OK, "ios14" is NOT OK.</remarks> | ||
| public static bool IsOSPlatformEarlierThan(string platformName) | ||
| { | ||
| (OSPlatform platform, Version version) = Parse(platformName); | ||
|
|
||
| return IsOSPlatformEarlierThan(platform, version.Major, version.Minor, version.Build, version.Revision); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Check for the OS with a < version comparison. Used to guard APIs that were obsoleted or removed in the given OS release. | ||
| /// </summary> | ||
| public static bool IsOSPlatformEarlierThan(OSPlatform osPlatform, int major) | ||
| => IsOSPlatform(osPlatform) && Environment.OSVersion.Version.Major < major; | ||
|
|
||
| /// <summary> | ||
| /// Check for the OS with a < version comparison. Used to guard APIs that were obsoleted or removed in the given OS release. | ||
| /// </summary> | ||
| public static bool IsOSPlatformEarlierThan(OSPlatform osPlatform, int major, int minor) | ||
| => IsOSPlatform(osPlatform) && !IsOSVersionOrLater(major, minor, int.MinValue, int.MinValue); | ||
|
|
||
| /// <summary> | ||
| /// Check for the OS with a < version comparison. Used to guard APIs that were obsoleted or removed in the given OS release. | ||
| /// </summary> | ||
| public static bool IsOSPlatformEarlierThan(OSPlatform osPlatform, int major, int minor, int build) | ||
| => IsOSPlatform(osPlatform) && !IsOSVersionOrLater(major, minor, build, int.MinValue); | ||
|
|
||
| /// <summary> | ||
| /// Check for the OS with a < version comparison. Used to guard APIs that were obsoleted or removed in the given OS release. | ||
| /// </summary> | ||
| public static bool IsOSPlatformEarlierThan(OSPlatform osPlatform, int major, int minor, int build, int revision) | ||
| => IsOSPlatform(osPlatform) && !IsOSVersionOrLater(major, minor, build, revision); | ||
|
|
||
| private static bool IsOSVersionOrLater(int major, int minor, int build, int revision) | ||
| { | ||
| Version current = Environment.OSVersion.Version; | ||
| if (current.Major != major) | ||
| { | ||
| return current.Major > major; | ||
| } | ||
| if (current.Minor != minor) | ||
| { | ||
| return current.Minor > minor; | ||
| } | ||
| if (current.Build != build) | ||
| { | ||
| return current.Build > build; | ||
| } | ||
|
|
||
| return current.Revision >= revision; | ||
| } | ||
|
|
||
| private static (OSPlatform, Version) Parse(string platformName) | ||
| { | ||
| if (platformName == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(platformName)); | ||
| } | ||
| if (platformName.Length == 0) | ||
| { | ||
| throw new ArgumentException(SR.Argument_EmptyValue, nameof(platformName)); | ||
| } | ||
|
|
||
| // iterate from the begining, as digits in the middle of the names are not supported by design | ||
| for (int i = 0; i < platformName.Length; i++) | ||
jeffhandley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| if (char.IsDigit(platformName[i])) | ||
| { | ||
| if (i > 0 && Version.TryParse(platformName.AsSpan(i), out Version? version)) | ||
| { | ||
| return (OSPlatform.Create(platformName.Substring(0, i)), version); | ||
adamsitnik marked this conversation as resolved.
Show resolved
Hide resolved
LuisCesarCosta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| else | ||
| { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| throw new ArgumentException(SR.Format(SR.Argument_InvalidPlatfromName, platformName), nameof(platformName)); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.