diff --git a/src/libraries/Common/tests/System/Collections/DebugView.Tests.cs b/src/libraries/Common/tests/System/Collections/DebugView.Tests.cs index 07b447c4c4de0f..b908cf13595a32 100644 --- a/src/libraries/Common/tests/System/Collections/DebugView.Tests.cs +++ b/src/libraries/Common/tests/System/Collections/DebugView.Tests.cs @@ -166,6 +166,9 @@ private static IEnumerable TestDebuggerAttributes_ListInputs() yield return new object[] { new SortedList() }; yield return new object[] { new SortedSet() }; yield return new object[] { new Stack() }; +#if !NETFRAMEWORK + yield return new object[] { new OrderedDictionary() }; +#endif yield return new object[] { new Dictionary().Keys }; yield return new object[] { new Dictionary().Values }; @@ -193,6 +196,10 @@ private static IEnumerable TestDebuggerAttributes_ListInputs() stack.Push(2); yield return new object[] { stack }; +#if !NETFRAMEWORK + yield return new object[] { new OrderedDictionary { { "One", "1" }, { "Two", "2" } } }; +#endif + yield return new object[] { new SortedList { { "One", 1 }, { "Two", 2 } }.Keys }; yield return new object[] { new SortedList { { 1f, 1L }, { 2f, 2L } }.Values }; @@ -277,6 +284,33 @@ public static void TestDebuggerAttributes_Null(object obj) Assert.IsType(tie.InnerException); } +#if !NETFRAMEWORK + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))] + public static void TestDebuggerAttributes_OrderedDictionary() + { + // Validate that OrderedDictionary displays as a list of KeyValuePairs with implicit indexing + var dict = new OrderedDictionary { { "One", "1" }, { "Two", "2" }, { "Three", "3" } }; + + DebuggerAttributes.ValidateDebuggerDisplayReferences(dict); + DebuggerAttributeInfo info = DebuggerAttributes.ValidateDebuggerTypeProxyProperties(dict); + PropertyInfo itemProperty = info.Properties.Single(pr => pr.GetCustomAttribute().State == DebuggerBrowsableState.RootHidden); + + // The debug view should return a KeyValuePair array + var items = itemProperty.GetValue(info.Instance) as Array; + Assert.NotNull(items); + Assert.Equal(3, items.Length); + + // Verify the items are KeyValuePairs in the correct order + var kvpArray = items.Cast>().ToArray(); + Assert.Equal("One", kvpArray[0].Key); + Assert.Equal("1", kvpArray[0].Value); + Assert.Equal("Two", kvpArray[1].Key); + Assert.Equal("2", kvpArray[1].Value); + Assert.Equal("Three", kvpArray[2].Key); + Assert.Equal("3", kvpArray[2].Value); + } +#endif + private class CustomKeyedCollection : KeyedCollection where TKey : notnull { public CustomKeyedCollection() : base() diff --git a/src/libraries/System.Collections/src/System.Collections.csproj b/src/libraries/System.Collections/src/System.Collections.csproj index bb7cfe2ad83cfb..54a2c4e9e694bc 100644 --- a/src/libraries/System.Collections/src/System.Collections.csproj +++ b/src/libraries/System.Collections/src/System.Collections.csproj @@ -17,6 +17,7 @@ + diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/OrderedDictionary.cs b/src/libraries/System.Collections/src/System/Collections/Generic/OrderedDictionary.cs index 8eec08838be6ba..7e806774efbbf3 100644 --- a/src/libraries/System.Collections/src/System/Collections/Generic/OrderedDictionary.cs +++ b/src/libraries/System.Collections/src/System/Collections/Generic/OrderedDictionary.cs @@ -23,7 +23,7 @@ namespace System.Collections.Generic /// Operations on the collection have algorithmic complexities that are similar to that of the /// class, except with lookups by key similar in complexity to that of . /// - [DebuggerTypeProxy(typeof(IDictionaryDebugView<,>))] + [DebuggerTypeProxy(typeof(OrderedDictionaryDebugView<,>))] [DebuggerDisplay("Count = {Count}")] #if SYSTEM_COLLECTIONS public diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/OrderedDictionaryDebugView.cs b/src/libraries/System.Collections/src/System/Collections/Generic/OrderedDictionaryDebugView.cs new file mode 100644 index 00000000000000..a982215c7ef186 --- /dev/null +++ b/src/libraries/System.Collections/src/System/Collections/Generic/OrderedDictionaryDebugView.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Diagnostics; + +namespace System.Collections.Generic +{ + internal sealed class OrderedDictionaryDebugView where TKey : notnull + { + private readonly OrderedDictionary _dictionary; + + public OrderedDictionaryDebugView(OrderedDictionary dictionary) + { + ArgumentNullException.ThrowIfNull(dictionary); + _dictionary = dictionary; + } + + [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] + public KeyValuePair[] Items + { + get + { + KeyValuePair[] items = new KeyValuePair[_dictionary.Count]; + ((ICollection>)_dictionary).CopyTo(items, 0); + return items; + } + } + } +} diff --git a/src/libraries/System.Text.Json/src/System.Text.Json.csproj b/src/libraries/System.Text.Json/src/System.Text.Json.csproj index cf127ba8b569a5..9f114fc6d1536b 100644 --- a/src/libraries/System.Text.Json/src/System.Text.Json.csproj +++ b/src/libraries/System.Text.Json/src/System.Text.Json.csproj @@ -375,6 +375,7 @@ The System.Text.Json library is built-in as part of the shared framework in .NET +