Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 525a6d7

Browse files
authored
KeyedCollection debug view doesn't work (#96261)
* KeyedCollection debug view doesn't work * fix remarks 1 * fix remarks 2 * add tests
1 parent 3467066 commit 525a6d7

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

src/libraries/Common/tests/System/Collections/DebugView.Tests.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ private static IEnumerable<object[]> TestDebuggerAttributes_GenericDictionaries(
4747
new ("[\"Two\"]", "2"),
4848
}
4949
};
50+
CustomKeyedCollection<string, int> collection = new ();
51+
collection.GetKeyForItemHandler = value => (2 * value).ToString();
52+
collection.InsertItem(0, 1);
53+
collection.InsertItem(1, 3);
54+
yield return new object[] { collection,
55+
new KeyValuePair<string, string>[]
56+
{
57+
new ("[\"2\"]", "1"),
58+
new ("[\"6\"]", "3"),
59+
}
60+
};
5061
}
5162

5263
private static IEnumerable<object[]> TestDebuggerAttributes_NonGenericDictionaries()
@@ -173,7 +184,7 @@ public static IEnumerable<object[]> TestDebuggerAttributes_Inputs()
173184

174185
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))]
175186
[MemberData(nameof(TestDebuggerAttributes_InputsPresentedAsDictionary))]
176-
public static void TestDebuggerAttributes_Dictionary(IDictionary obj, KeyValuePair<string, string>[] expected)
187+
public static void TestDebuggerAttributes_Dictionary(object obj, KeyValuePair<string, string>[] expected)
177188
{
178189
DebuggerAttributes.ValidateDebuggerDisplayReferences(obj);
179190
DebuggerAttributeInfo info = DebuggerAttributes.ValidateDebuggerTypeProxyProperties(obj);
@@ -206,5 +217,32 @@ public static void TestDebuggerAttributes_Null(object obj)
206217
TargetInvocationException tie = Assert.Throws<TargetInvocationException>(() => Activator.CreateInstance(proxyType, (object)null));
207218
Assert.IsType<ArgumentNullException>(tie.InnerException);
208219
}
220+
221+
private class CustomKeyedCollection<TKey, TValue> : KeyedCollection<TKey, TValue> where TKey : notnull
222+
{
223+
public CustomKeyedCollection() : base()
224+
{
225+
}
226+
227+
public CustomKeyedCollection(IEqualityComparer<TKey> comparer) : base(comparer)
228+
{
229+
}
230+
231+
public CustomKeyedCollection(IEqualityComparer<TKey> comparer, int dictionaryCreationThreshold) : base(comparer, dictionaryCreationThreshold)
232+
{
233+
}
234+
235+
public Func<TValue, TKey> GetKeyForItemHandler { get; set; }
236+
237+
protected override TKey GetKeyForItem(TValue item)
238+
{
239+
return GetKeyForItemHandler(item);
240+
}
241+
242+
public new void InsertItem(int index, TValue item)
243+
{
244+
base.InsertItem(index, item);
245+
}
246+
}
209247
}
210248
}

src/libraries/System.ObjectModel/src/System/Collections/ObjectModel/KeyedCollection.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace System.Collections.ObjectModel
99
{
1010
[Serializable]
11-
[DebuggerTypeProxy(typeof(CollectionDebugView<>))]
11+
[DebuggerTypeProxy(typeof(KeyedCollection<,>.KeyedCollectionDebugView))]
1212
[DebuggerDisplay("Count = {Count}")]
1313
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
1414
public abstract class KeyedCollection<TKey, TItem> : Collection<TItem> where TKey : notnull
@@ -280,5 +280,47 @@ private void RemoveKey(TKey key)
280280
keyCount--;
281281
}
282282
}
283+
284+
private sealed class KeyedCollectionDebugView
285+
{
286+
private readonly KeyedCollection<TKey, TItem> _collection;
287+
288+
public KeyedCollectionDebugView(KeyedCollection<TKey, TItem> collection)
289+
{
290+
ArgumentNullException.ThrowIfNull(collection);
291+
_collection = collection;
292+
}
293+
294+
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
295+
public KeyedCollectionDebugViewItem[] Items
296+
{
297+
get
298+
{
299+
var items = new KeyedCollectionDebugViewItem[_collection.Count];
300+
for (int i = 0; i < items.Length; i++)
301+
{
302+
TItem item = _collection[i];
303+
items[i] = new KeyedCollectionDebugViewItem(_collection.GetKeyForItem(item), item);
304+
}
305+
return items;
306+
}
307+
}
308+
309+
[DebuggerDisplay("{Value}", Name = "[{Key}]")]
310+
internal readonly struct KeyedCollectionDebugViewItem
311+
{
312+
public KeyedCollectionDebugViewItem(TKey key, TItem value)
313+
{
314+
Key = key;
315+
Value = value;
316+
}
317+
318+
[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
319+
public TKey Key { get; }
320+
321+
[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
322+
public TItem Value { get; }
323+
}
324+
}
283325
}
284326
}

0 commit comments

Comments
 (0)