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

Improve and unify debug views of dictionaries. #92534

Merged
merged 11 commits into from
Nov 2, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<Compile Include="System\Collections\SortedList.cs" />
<Compile Include="System\Collections\Stack.cs" />
<Compile Include="System\Collections\Specialized\CollectionsUtil.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\KeyValuePairs.cs"
Link="Common\System\Collections\KeyValuePairs.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\DebugViewDictionaryItem.cs"
Link="Common\System\Collections\Generic\DebugViewDictionaryItem.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Runtime" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
**
===========================================================*/

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
Expand Down Expand Up @@ -351,12 +352,12 @@ public virtual void CopyTo(Array array, int arrayIndex)
// KeyValuePairs is different from Dictionary Entry in that it has special
// debugger attributes on its fields.

internal virtual KeyValuePairs[] ToKeyValuePairsArray()
internal virtual DebugViewDictionaryItem<object, object?>[] ToDebugViewDictionaryItemArray()
{
KeyValuePairs[] array = new KeyValuePairs[Count];
var array = new DebugViewDictionaryItem<object, object?>[Count];
for (int i = 0; i < Count; i++)
{
array[i] = new KeyValuePairs(keys[i], values[i]);
array[i] = new DebugViewDictionaryItem<object, object?>(keys[i], values[i]);
}
return array;
}
Expand Down Expand Up @@ -766,9 +767,9 @@ public override void SetByIndex(int index, object? value)
}
}

internal override KeyValuePairs[] ToKeyValuePairsArray()
internal override DebugViewDictionaryItem<object, object?>[] ToDebugViewDictionaryItemArray()
{
return _list.ToKeyValuePairsArray();
return _list.ToDebugViewDictionaryItemArray();
}

public override void TrimToSize()
Expand Down Expand Up @@ -1097,11 +1098,11 @@ public SortedListDebugView(SortedList sortedList)
}

[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public KeyValuePairs[] Items
public DebugViewDictionaryItem<object, object?>[] Items
{
get
{
return _sortedList.ToKeyValuePairsArray();
return _sortedList.ToDebugViewDictionaryItemArray();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="System\Collections\BitArray.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\DebugViewDictionaryItem.cs"
Link="Common\System\Collections\Generic\DebugViewDictionaryItem.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ICollectionDebugView.cs"
Link="Common\System\Collections\Generic\ICollectionDebugView.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\IDictionaryDebugView.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\CollectionExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\Comparer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\Dictionary.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\DebugViewDictionaryItem.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\EqualityComparer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\HashSet.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\Generic\HashSetEqualityComparer.cs" />
Expand Down Expand Up @@ -225,7 +226,6 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\IList.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\IStructuralComparable.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\IStructuralEquatable.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\KeyValuePairs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\ListDictionaryInternal.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\ObjectModel\Collection.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Collections\ObjectModel\CollectionHelpers.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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
{
/// <summary>
/// Defines a key/value pair for displaying an item of a dictionary by a debugger.
/// </summary>
[DebuggerDisplay("{Value}", Name = "[{Key}]")]
internal readonly struct DebugViewDictionaryItem<K, V>
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
{
public DebugViewDictionaryItem(K key, V value)
{
Key = key;
Value = value;
}

public DebugViewDictionaryItem(KeyValuePair<K, V> keyValue)
{
Key = keyValue.Key;
Value = keyValue.Value;
}

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

[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
public V Value { get; init; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ public IDictionaryDebugView(IDictionary<K, V> dictionary)
}

[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public KeyValuePair<K, V>[] Items
public DebugViewDictionaryItem<K, V>[] Items
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
{
get
{
KeyValuePair<K, V>[] items = new KeyValuePair<K, V>[_dict.Count];
_dict.CopyTo(items, 0);
var keyValuePairs = new KeyValuePair<K, V>[_dict.Count];
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
_dict.CopyTo(keyValuePairs, 0);
var items = new DebugViewDictionaryItem<K, V>[keyValuePairs.Length];
for (int i = 0; i < items.Length; i++)
{
items[i] = new DebugViewDictionaryItem<K, V>(keyValuePairs[i]);
}
return items;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
**
===========================================================*/

using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -581,21 +582,17 @@ public virtual void CopyTo(Array array, int arrayIndex)
CopyEntries(array, arrayIndex);
}

// Copies the values in this Hashtable to an KeyValuePairs array.
// KeyValuePairs is different from Dictionary Entry in that it has special
// debugger attributes on its fields.

internal virtual KeyValuePairs[] ToKeyValuePairsArray()
internal virtual DebugViewDictionaryItem<object, object?>[] ToKeyValuePairsArray()
{
KeyValuePairs[] array = new KeyValuePairs[_count];
var array = new DebugViewDictionaryItem<object, object?>[_count];
int index = 0;
Bucket[] lbuckets = _buckets;
for (int i = lbuckets.Length; --i >= 0;)
{
object? keyv = lbuckets[i].key;
if ((keyv != null) && (keyv != _buckets))
{
array[index++] = new KeyValuePairs(keyv, lbuckets[i].val);
array[index++] = new DebugViewDictionaryItem<object, object?>(keyv, lbuckets[i].val);
}
}

Expand Down Expand Up @@ -1385,7 +1382,7 @@ public override void OnDeserialization(object? sender)
// call OnDeserialization on our parent table.
}

internal override KeyValuePairs[] ToKeyValuePairsArray()
internal override DebugViewDictionaryItem<object, object?>[] ToKeyValuePairsArray()
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
{
return _table.ToKeyValuePairsArray();
}
Expand Down Expand Up @@ -1509,7 +1506,7 @@ public HashtableDebugView(Hashtable hashtable)
}

[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public KeyValuePairs[] Items => _hashtable.ToKeyValuePairsArray();
public DebugViewDictionaryItem<object, object?>[] Items => _hashtable.ToKeyValuePairsArray();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;

Expand Down Expand Up @@ -418,15 +419,15 @@ public ListDictionaryInternalDebugView(ListDictionaryInternal list)
}

[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public KeyValuePairs[] Items
public DebugViewDictionaryItem<object, object?>[] Items
{
get
{
var array = new KeyValuePairs[_list.count];
var array = new DebugViewDictionaryItem<object, object?>[_list.count];
int index = 0;
for (DictionaryNode? node = _list.head; node != null; node = node.next)
{
array[index++] = new KeyValuePairs(node.key, node.value);
array[index++] = new DebugViewDictionaryItem<object, object?>(node.key, node.value);
}
return array;
}
Expand Down
Loading