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
Changes from 5 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
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; }
}
}
}
}