From ab68c24f1848d696b27b4fefb05df29b0a00a303 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Wed, 6 Aug 2025 02:49:41 +0100 Subject: [PATCH 1/5] Refactor `NET8_0`/`NET9_0` directives for clarity and maintainability --- .../src/System/Buffers/Text/Base64Helper/Base64Helper.cs | 2 +- .../src/System/Text/Json/Nodes/JsonObject.IDictionary.cs | 6 +++--- .../src/System/Text/Json/Nodes/JsonObject.cs | 6 +++--- .../Metadata/DefaultJsonTypeInfoResolver.Helpers.cs | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64Helper.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64Helper.cs index 62f99501bfaa46..03741847db0492 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64Helper.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64Helper.cs @@ -71,7 +71,7 @@ internal static unsafe void AssertWrite(ushort* dest, ushort* destStart } } -#if NET8_0 +#if NET && !NET9_0_OR_GREATER [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static bool VectorContainsNonAsciiChar(Vector128 utf16Vector) { diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.IDictionary.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.IDictionary.cs index f86362c9eea2ca..08cb931a946380 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.IDictionary.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.IDictionary.cs @@ -58,11 +58,11 @@ public bool TryAdd(string propertyName, JsonNode? value, out int index) { ThrowHelper.ThrowArgumentNullException(nameof(propertyName)); } -#if NET9_0 +#if NET10_0_OR_GREATER + bool success = Dictionary.TryAdd(propertyName, value, out index); +#else bool success = Dictionary.TryAdd(propertyName, value); index = success ? Dictionary.Count - 1 : Dictionary.IndexOf(propertyName); -#else - bool success = Dictionary.TryAdd(propertyName, value, out index); #endif if (success) { diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs index ebab8d554de25e..309c1751759a95 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs @@ -142,7 +142,9 @@ public bool TryGetPropertyValue(string propertyName, out JsonNode? jsonNode, out { ArgumentNullException.ThrowIfNull(propertyName); -#if NET9_0 +#if NET10_0_OR_GREATER + return Dictionary.TryGetValue(propertyName, out jsonNode, out index); +#else index = Dictionary.IndexOf(propertyName); if (index < 0) { @@ -152,8 +154,6 @@ public bool TryGetPropertyValue(string propertyName, out JsonNode? jsonNode, out jsonNode = Dictionary.GetAt(index).Value; return true; -#else - return Dictionary.TryGetValue(propertyName, out jsonNode, out index); #endif } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Helpers.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Helpers.cs index d7aef7399c0b49..90f203c7233def 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Helpers.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Helpers.cs @@ -497,7 +497,7 @@ private static NullabilityState DetermineParameterNullability(ParameterInfo para { return NullabilityState.NotNull; } -#if NET8_0 +#if NET && !NET9_0_OR_GREATER // Workaround for https://github.com/dotnet/runtime/issues/92487 // The fix has been incorporated into .NET 9 (and the polyfilled implementations in netfx). // Should be removed once .NET 8 support is dropped. From da6a5b5f2bf040835dee3350c1ce6112629aef1d Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Wed, 6 Aug 2025 11:49:14 +0100 Subject: [PATCH 2/5] Update `Base64Helper.cs` --- .../src/System/Buffers/Text/Base64Helper/Base64Helper.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64Helper.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64Helper.cs index 03741847db0492..6acacb6051c25e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64Helper.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64Helper.cs @@ -4,7 +4,7 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; -#if NET +#if NET8_0 using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.Arm; using System.Runtime.Intrinsics.X86; @@ -71,7 +71,7 @@ internal static unsafe void AssertWrite(ushort* dest, ushort* destStart } } -#if NET && !NET9_0_OR_GREATER +#if NET8_0 [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static bool VectorContainsNonAsciiChar(Vector128 utf16Vector) { From 8ae32434dd09d390c11be3adc6914dcb2680dba0 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Wed, 6 Aug 2025 11:50:42 +0100 Subject: [PATCH 3/5] Update `JsonObject` --- .../Text/Json/Nodes/JsonObject.IDictionary.cs | 6 +++--- .../src/System/Text/Json/Nodes/JsonObject.cs | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.IDictionary.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.IDictionary.cs index 08cb931a946380..f86362c9eea2ca 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.IDictionary.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.IDictionary.cs @@ -58,11 +58,11 @@ public bool TryAdd(string propertyName, JsonNode? value, out int index) { ThrowHelper.ThrowArgumentNullException(nameof(propertyName)); } -#if NET10_0_OR_GREATER - bool success = Dictionary.TryAdd(propertyName, value, out index); -#else +#if NET9_0 bool success = Dictionary.TryAdd(propertyName, value); index = success ? Dictionary.Count - 1 : Dictionary.IndexOf(propertyName); +#else + bool success = Dictionary.TryAdd(propertyName, value, out index); #endif if (success) { diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs index 309c1751759a95..be00902ce449f4 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs @@ -142,9 +142,7 @@ public bool TryGetPropertyValue(string propertyName, out JsonNode? jsonNode, out { ArgumentNullException.ThrowIfNull(propertyName); -#if NET10_0_OR_GREATER - return Dictionary.TryGetValue(propertyName, out jsonNode, out index); -#else +#if NET9_0 index = Dictionary.IndexOf(propertyName); if (index < 0) { @@ -154,6 +152,8 @@ public bool TryGetPropertyValue(string propertyName, out JsonNode? jsonNode, out jsonNode = Dictionary.GetAt(index).Value; return true; +#else + return Dictionary.TryGetValue(propertyName, out jsonNode, out index); #endif } @@ -267,14 +267,14 @@ internal void SetItem(string propertyName, JsonNode? value) OrderedDictionary dict = Dictionary; if ( -#if NET10_0_OR_GREATER - !dict.TryAdd(propertyName, value, out int index) -#else +#if NET9_0 !dict.TryAdd(propertyName, value) +#else + !dict.TryAdd(propertyName, value, out int index) #endif ) { -#if !NET10_0_OR_GREATER +#if NET9_0 int index = dict.IndexOf(propertyName); #endif Debug.Assert(index >= 0); From 2a6bc32732137dbad84065ab9420e687c59fa523 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Wed, 6 Aug 2025 11:51:45 +0100 Subject: [PATCH 4/5] revert `DefaultJsonTypeInfoResolver.Helpers.cs` --- .../Metadata/DefaultJsonTypeInfoResolver.Helpers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Helpers.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Helpers.cs index 90f203c7233def..d7aef7399c0b49 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Helpers.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Helpers.cs @@ -497,7 +497,7 @@ private static NullabilityState DetermineParameterNullability(ParameterInfo para { return NullabilityState.NotNull; } -#if NET && !NET9_0_OR_GREATER +#if NET8_0 // Workaround for https://github.com/dotnet/runtime/issues/92487 // The fix has been incorporated into .NET 9 (and the polyfilled implementations in netfx). // Should be removed once .NET 8 support is dropped. From fb61f2c93eb0b47a7328faad0e3d03f20c08a563 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Wed, 6 Aug 2025 13:58:48 +0100 Subject: [PATCH 5/5] revert `Base64Helper.cs` --- .../src/System/Buffers/Text/Base64Helper/Base64Helper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64Helper.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64Helper.cs index 6acacb6051c25e..62f99501bfaa46 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64Helper.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64Helper.cs @@ -4,7 +4,7 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; -#if NET8_0 +#if NET using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.Arm; using System.Runtime.Intrinsics.X86;