Skip to content
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

KeyedCollection debug view doesn't work #96261

Merged
merged 6 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think some of that indirection could be removed if you just made the derived type non-generic and inlined the GetKeyBehavior in the implementation.


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
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
{
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; }
}
}
}
}