Skip to content

Commit 154177c

Browse files
authored
Improve debug views of dictionaries (#52867)
1 parent 640e122 commit 154177c

File tree

36 files changed

+177
-98
lines changed

36 files changed

+177
-98
lines changed

src/Components/Components/src/Microsoft.AspNetCore.Components.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
<Compile Include="$(ComponentsSharedSourceRoot)src\HotReloadManager.cs" LinkBase="HotReload" />
2020
<Compile Include="$(SharedSourceRoot)LinkerFlags.cs" LinkBase="Shared" />
2121
<Compile Include="$(SharedSourceRoot)QueryStringEnumerable.cs" LinkBase="Shared" />
22+
<Compile Include="$(SharedSourceRoot)Debugger\DictionaryItemDebugView.cs" LinkBase="Shared" />
23+
<Compile Include="$(SharedSourceRoot)Debugger\DictionaryDebugView.cs" LinkBase="Shared" />
2224
</ItemGroup>
2325

2426
<Import Project="Microsoft.AspNetCore.Components.Routing.targets" />
25-
27+
2628
<ItemGroup>
2729
<Reference Include="Microsoft.Extensions.Logging.Abstractions" />
2830
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />

src/Extensions/Features/src/FeatureCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,6 @@ private sealed class FeatureCollectionDebugView(FeatureCollection features)
161161
private readonly FeatureCollection _features = features;
162162

163163
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
164-
public KeyValuePair<string, object>[] Items => _features.Select(pair => new KeyValuePair<string, object>(pair.Key.FullName ?? string.Empty, pair.Value)).ToArray();
164+
public DictionaryItemDebugView<Type, object>[] Items => _features.Select(pair => new DictionaryItemDebugView<Type, object>(pair)).ToArray();
165165
}
166166
}

src/Extensions/Features/src/Microsoft.Extensions.Features.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Microsoft.AspNetCore.Http.Features.FeatureCollection
1919
<Compile Include="$(SharedSourceRoot)ThrowHelpers\ArgumentNullThrowHelper.cs" LinkBase="Shared" />
2020
<Compile Include="$(SharedSourceRoot)ThrowHelpers\ArgumentOutOfRangeThrowHelper.cs" LinkBase="Shared" />
2121
<Compile Include="$(SharedSourceRoot)CallerArgument\CallerArgumentExpressionAttribute.cs" LinkBase="Shared" />
22+
<Compile Include="$(SharedSourceRoot)Debugger\DictionaryItemDebugView.cs" LinkBase="Shared" />
2223
</ItemGroup>
2324

2425
</Project>

src/Http/Http.Abstractions/src/HttpContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,6 @@ private sealed class HttpContextFeatureDebugView(IFeatureCollection features)
108108
private readonly IFeatureCollection _features = features;
109109

110110
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
111-
public KeyValuePair<string, object>[] Items => _features.Select(pair => new KeyValuePair<string, object>(pair.Key.FullName ?? string.Empty, pair.Value)).ToArray();
111+
public DictionaryItemDebugView<Type, object>[] Items => _features.Select(pair => new DictionaryItemDebugView<Type, object>(pair)).ToArray();
112112
}
113113
}

src/Http/Http.Abstractions/src/Microsoft.AspNetCore.Http.Abstractions.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Microsoft.AspNetCore.Http.HttpResponse</Description>
2929
<Compile Include="$(SharedSourceRoot)Reroute.cs" />
3030
<Compile Include="$(SharedSourceRoot)Debugger\HttpContextDebugFormatter.cs" LinkBase="Shared" />
3131
<Compile Include="$(SharedSourceRoot)Debugger\DebuggerHelpers.cs" LinkBase="Shared" />
32+
<Compile Include="$(SharedSourceRoot)Debugger\DictionaryItemDebugView.cs" LinkBase="Shared" />
33+
<Compile Include="$(SharedSourceRoot)Debugger\DictionaryDebugView.cs" LinkBase="Shared" />
3234
</ItemGroup>
3335

3436
<ItemGroup>

src/Http/Http.Abstractions/src/Routing/RouteValueDictionary.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
using System.Collections;
55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
7-
using System.Linq;
87
using System.Runtime.CompilerServices;
8+
using Microsoft.AspNetCore.Shared;
9+
910
#if !COMPONENTS
1011
using System.Collections.Concurrent;
1112
using System.Reflection.Metadata;
@@ -29,7 +30,7 @@ namespace Microsoft.AspNetCore.Routing;
2930
/// An <see cref="IDictionary{String, Object}"/> type for route values.
3031
/// </summary>
3132
#endif
32-
[DebuggerTypeProxy(typeof(RouteValueDictionaryDebugView))]
33+
[DebuggerTypeProxy(typeof(DictionaryDebugView<string, object?>))]
3334
[DebuggerDisplay("Count = {Count}")]
3435
#if !COMPONENTS
3536
public class RouteValueDictionary : IDictionary<string, object?>, IReadOnlyDictionary<string, object?>
@@ -929,12 +930,4 @@ internal static void ClearCache(Type[]? _)
929930
}
930931
}
931932
#endif
932-
933-
private sealed class RouteValueDictionaryDebugView(RouteValueDictionary dictionary)
934-
{
935-
private readonly RouteValueDictionary _dictionary = dictionary;
936-
937-
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
938-
public KeyValuePair<string, object?>[] Items => _dictionary.ToArray();
939-
}
940933
}

src/Http/Http/src/FormCollection.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections;
5+
using System.Diagnostics;
6+
using Microsoft.AspNetCore.Shared;
57
using Microsoft.Extensions.Primitives;
68

79
namespace Microsoft.AspNetCore.Http;
810

911
/// <summary>
1012
/// Contains the parsed HTTP form values.
1113
/// </summary>
14+
[DebuggerDisplay("Count = {Count}")]
15+
[DebuggerTypeProxy(typeof(StringValuesDictionaryDebugView))]
1216
public class FormCollection : IFormCollection
1317
{
1418
/// <summary>

src/Http/Http/src/HeaderDictionary.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Collections;
55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
7-
using System.Linq;
7+
using Microsoft.AspNetCore.Shared;
88
using Microsoft.Extensions.Primitives;
99
using Microsoft.Net.Http.Headers;
1010

@@ -13,8 +13,8 @@ namespace Microsoft.AspNetCore.Http;
1313
/// <summary>
1414
/// Represents a wrapper for RequestHeaders and ResponseHeaders.
1515
/// </summary>
16-
[DebuggerTypeProxy(typeof(HeaderDictionaryDebugView))]
1716
[DebuggerDisplay("{DebuggerToString(),nq}")]
17+
[DebuggerTypeProxy(typeof(StringValuesDictionaryDebugView))]
1818
public class HeaderDictionary : IHeaderDictionary
1919
{
2020
private static readonly string[] EmptyKeys = Array.Empty<string>();
@@ -455,12 +455,4 @@ void IEnumerator.Reset()
455455
}
456456
}
457457
}
458-
459-
private sealed class HeaderDictionaryDebugView(HeaderDictionary dictionary)
460-
{
461-
private readonly HeaderDictionary _dictionary = dictionary;
462-
463-
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
464-
public KeyValuePair<string, string>[] Items => _dictionary.Select(pair => new KeyValuePair<string, string>(pair.Key, pair.Value.ToString())).ToArray();
465-
}
466458
}

src/Http/Http/src/Internal/ItemsDictionary.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
using System.Collections;
55
using System.Diagnostics;
66
using System.Diagnostics.CodeAnalysis;
7-
using System.Linq;
7+
using Microsoft.AspNetCore.Shared;
88

99
namespace Microsoft.AspNetCore.Http;
1010

11-
[DebuggerTypeProxy(typeof(ItemsDictionaryDebugView))]
11+
[DebuggerTypeProxy(typeof(DictionaryDebugView<object, object>))]
1212
[DebuggerDisplay("Count = {Items.Count}")]
1313
internal sealed class ItemsDictionary : IDictionary<object, object?>
1414
{
@@ -142,7 +142,7 @@ private void EnsureDictionary()
142142

143143
private sealed class EmptyEnumerator : IEnumerator<KeyValuePair<object, object?>>
144144
{
145-
// In own class so only initalized if GetEnumerator is called on an empty ItemsDictionary
145+
// In own class so only initialized if GetEnumerator is called on an empty ItemsDictionary
146146
public static readonly IEnumerator<KeyValuePair<object, object?>> Instance = new EmptyEnumerator();
147147
public KeyValuePair<object, object?> Current => default;
148148

@@ -159,16 +159,8 @@ public void Reset()
159159

160160
private static class EmptyDictionary
161161
{
162-
// In own class so only initalized if CopyTo is called on an empty ItemsDictionary
162+
// In own class so only initialized if CopyTo is called on an empty ItemsDictionary
163163
public static readonly IDictionary<object, object?> Dictionary = new Dictionary<object, object?>();
164164
public static ICollection<KeyValuePair<object, object?>> Collection => Dictionary;
165165
}
166-
167-
private sealed class ItemsDictionaryDebugView(ItemsDictionary dictionary)
168-
{
169-
private readonly ItemsDictionary _dictionary = dictionary;
170-
171-
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
172-
public KeyValuePair<object, object?>[] Items => _dictionary.ToArray();
173-
}
174166
}

src/Http/Http/src/Internal/RequestCookieCollection.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Diagnostics.CodeAnalysis;
77
using System.Linq;
88
using Microsoft.AspNetCore.Internal;
9+
using Microsoft.AspNetCore.Shared;
910
using Microsoft.Extensions.Primitives;
1011
using Microsoft.Net.Http.Headers;
1112

@@ -231,6 +232,6 @@ private sealed class RequestCookieCollectionDebugView(RequestCookieCollection co
231232
private readonly RequestCookieCollection _collection = collection;
232233

233234
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
234-
public KeyValuePair<string, string>[] Items => _collection.ToArray();
235+
public DictionaryItemDebugView<string, string>[] Items => _collection.Select(pair => new DictionaryItemDebugView<string, string>(pair)).ToArray();
235236
}
236237
}

0 commit comments

Comments
 (0)