Improve StringValues, StringSegment and IChangeToken debugging (#88960)
authorJames Newton-King <james@newtonking.com>
Sun, 16 Jul 2023 18:42:08 +0000 (02:42 +0800)
committerGitHub <noreply@github.com>
Sun, 16 Jul 2023 18:42:08 +0000 (11:42 -0700)
src/libraries/Microsoft.Extensions.Primitives/src/CancellationChangeToken.cs
src/libraries/Microsoft.Extensions.Primitives/src/CompositeChangeToken.cs
src/libraries/Microsoft.Extensions.Primitives/src/StringSegment.cs
src/libraries/Microsoft.Extensions.Primitives/src/StringValues.cs

index 6beb90d..eb024d6 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 
 using System;
+using System.Diagnostics;
 using System.Threading;
 using Microsoft.Extensions.Internal;
 
@@ -10,6 +11,7 @@ namespace Microsoft.Extensions.Primitives
     /// <summary>
     /// A <see cref="IChangeToken"/> implementation using <see cref="CancellationToken"/>.
     /// </summary>
+    [DebuggerDisplay("HasChanged = {HasChanged}")]
     public class CancellationChangeToken : IChangeToken
     {
         /// <summary>
index 956c7bb..edfe38b 100644 (file)
@@ -12,6 +12,7 @@ namespace Microsoft.Extensions.Primitives
     /// <summary>
     /// An <see cref="IChangeToken"/> which represents one or more <see cref="IChangeToken"/> instances.
     /// </summary>
+    [DebuggerDisplay("HasChanged = {HasChanged}")]
     public class CompositeChangeToken : IChangeToken
     {
         private static readonly Action<object?> _onChangeDelegate = OnChange;
index f321de5..c78527d 100644 (file)
@@ -11,6 +11,7 @@ namespace Microsoft.Extensions.Primitives
     /// <summary>
     /// An optimized representation of a substring.
     /// </summary>
+    [DebuggerDisplay("{Value}")]
     public readonly struct StringSegment : IEquatable<StringSegment>, IEquatable<string?>
     {
         /// <summary>
index 2c19ee2..e153650 100644 (file)
@@ -14,6 +14,8 @@ namespace Microsoft.Extensions.Primitives
     /// <summary>
     /// Represents zero/null, one, or many strings in an efficient way.
     /// </summary>
+    [DebuggerDisplay("{ToString()}")]
+    [DebuggerTypeProxy(typeof(StringValuesDebugView))]
     public readonly struct StringValues : IList<string?>, IReadOnlyList<string?>, IEquatable<StringValues>, IEquatable<string?>, IEquatable<string?[]?>
     {
         /// <summary>
@@ -825,5 +827,11 @@ namespace Microsoft.Extensions.Primitives
             {
             }
         }
+
+        private sealed class StringValuesDebugView(StringValues values)
+        {
+            [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
+            public string?[] Items => values.ToArray();
+        }
     }
 }