Skip to content

Commit

Permalink
KeyedCollection debug view doesn't work (#96261)
Browse files Browse the repository at this point in the history
* KeyedCollection debug view doesn't work

* fix remarks 1

* fix remarks 2

* add tests
  • Loading branch information
pedrobsaila authored Jan 12, 2024
1 parent 3467066 commit 525a6d7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
40 changes: 39 additions & 1 deletion src/libraries/Common/tests/System/Collections/DebugView.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ private static IEnumerable<object[]> TestDebuggerAttributes_GenericDictionaries(
new ("[\"Two\"]", "2"),
}
};
CustomKeyedCollection<string, int> collection = new ();
collection.GetKeyForItemHandler = value => (2 * value).ToString();
collection.InsertItem(0, 1);
collection.InsertItem(1, 3);
yield return new object[] { collection,
new KeyValuePair<string, string>[]
{
new ("[\"2\"]", "1"),
new ("[\"6\"]", "3"),
}
};
}

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

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))]
[MemberData(nameof(TestDebuggerAttributes_InputsPresentedAsDictionary))]
public static void TestDebuggerAttributes_Dictionary(IDictionary obj, KeyValuePair<string, string>[] expected)
public static void TestDebuggerAttributes_Dictionary(object obj, KeyValuePair<string, string>[] expected)
{
DebuggerAttributes.ValidateDebuggerDisplayReferences(obj);
DebuggerAttributeInfo info = DebuggerAttributes.ValidateDebuggerTypeProxyProperties(obj);
Expand Down Expand Up @@ -206,5 +217,32 @@ public static void TestDebuggerAttributes_Null(object obj)
TargetInvocationException tie = Assert.Throws<TargetInvocationException>(() => Activator.CreateInstance(proxyType, (object)null));
Assert.IsType<ArgumentNullException>(tie.InnerException);
}

private class CustomKeyedCollection<TKey, TValue> : KeyedCollection<TKey, TValue> where TKey : notnull
{
public CustomKeyedCollection() : base()
{
}

public CustomKeyedCollection(IEqualityComparer<TKey> comparer) : base(comparer)
{
}

public CustomKeyedCollection(IEqualityComparer<TKey> comparer, int dictionaryCreationThreshold) : base(comparer, dictionaryCreationThreshold)
{
}

public Func<TValue, TKey> GetKeyForItemHandler { get; set; }

protected override TKey GetKeyForItem(TValue item)
{
return GetKeyForItemHandler(item);
}

public new void InsertItem(int index, TValue item)
{
base.InsertItem(index, item);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace System.Collections.ObjectModel
{
[Serializable]
[DebuggerTypeProxy(typeof(CollectionDebugView<>))]
[DebuggerTypeProxy(typeof(KeyedCollection<,>.KeyedCollectionDebugView))]
[DebuggerDisplay("Count = {Count}")]
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public abstract class KeyedCollection<TKey, TItem> : Collection<TItem> where TKey : notnull
Expand Down Expand Up @@ -280,5 +280,47 @@ private void RemoveKey(TKey key)
keyCount--;
}
}

private sealed class KeyedCollectionDebugView
{
private readonly KeyedCollection<TKey, TItem> _collection;

public KeyedCollectionDebugView(KeyedCollection<TKey, TItem> collection)
{
ArgumentNullException.ThrowIfNull(collection);
_collection = collection;
}

[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public KeyedCollectionDebugViewItem[] Items
{
get
{
var items = new KeyedCollectionDebugViewItem[_collection.Count];
for (int i = 0; i < items.Length; i++)
{
TItem item = _collection[i];
items[i] = new KeyedCollectionDebugViewItem(_collection.GetKeyForItem(item), item);
}
return items;
}
}

[DebuggerDisplay("{Value}", Name = "[{Key}]")]
internal readonly struct KeyedCollectionDebugViewItem
{
public KeyedCollectionDebugViewItem(TKey key, TItem value)
{
Key = key;
Value = value;
}

[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
public TKey Key { get; }

[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
public TItem Value { get; }
}
}
}
}

0 comments on commit 525a6d7

Please sign in to comment.