c# - DebuggerDisplay (DebuggerDisplayAttribute) with ",raw"? - Stack Overflow

admin2025-04-17  2

In the source code of System.ReadOnlyMemory<T> on GitHub I saw the following:

[DebuggerDisplay("{ToString(),raw}")]

Exactly what is that ,raw doing there?

Bonus points if you can point me to a complete reference of the format of the string accepted by the constructor of DebuggerDisplayAttribute. The official documentation of both the attribute and of its constructor is suspiciously silent on the subject.

Even the ,nq trick which is commonly used with DebuggerDisplay does not, as far as I can tell, appear in any kind of exhaustive, systematic, authoritative documentation; I have only seen it mentioned in some how-to guides in indirect, hand-wavy, wishy-washy terms.

There is this: "Format specifiers in C# in the Visual Studio debugger" but:

  1. I have been unable to find any documented relationship between the string accepted by the constructor of DebuggerDisplayAttribute and "Format specifiers in C# in the Visual Studio debugger".

  2. If I was to assume that the constructor of DebuggerDisplayAttribute accepts a string in the format documented in "Format specifiers in C# in the Visual Studio debugger", this still does not help me understand what ,raw does when used with DebuggerDisplayAttribute, because the explanation for ,raw says "Displays item as it appears in the raw item node. Valid on proxy objects only."

In the source code of System.ReadOnlyMemory<T> on GitHub I saw the following:

[DebuggerDisplay("{ToString(),raw}")]

Exactly what is that ,raw doing there?

Bonus points if you can point me to a complete reference of the format of the string accepted by the constructor of DebuggerDisplayAttribute. The official documentation of both the attribute and of its constructor is suspiciously silent on the subject.

Even the ,nq trick which is commonly used with DebuggerDisplay does not, as far as I can tell, appear in any kind of exhaustive, systematic, authoritative documentation; I have only seen it mentioned in some how-to guides in indirect, hand-wavy, wishy-washy terms.

There is this: "Format specifiers in C# in the Visual Studio debugger" but:

  1. I have been unable to find any documented relationship between the string accepted by the constructor of DebuggerDisplayAttribute and "Format specifiers in C# in the Visual Studio debugger".

  2. If I was to assume that the constructor of DebuggerDisplayAttribute accepts a string in the format documented in "Format specifiers in C# in the Visual Studio debugger", this still does not help me understand what ,raw does when used with DebuggerDisplayAttribute, because the explanation for ,raw says "Displays item as it appears in the raw item node. Valid on proxy objects only."

Share Improve this question edited Feb 1 at 14:40 Mike Nakis asked Feb 1 at 14:28 Mike NakisMike Nakis 62.3k11 gold badges124 silver badges168 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 4

Your question took me down to the rabbit hole of dotnet source code :D, thanks!

TLDR:

I don't have a definitive answer unfortunately, it seems to me that it is dependent on a debugger implementation. The one that is being used in visual studio is vsdbg should have the answers in its code, but if I'm not mistaken its code is closed. My gut feeling is that it isn't supported at all (I mean ,raw modifier)


I've started searching for ,nq and ,raw through open repositories on github in a hope that it will lead me to something.

Search under dotnet gave me following:

Found some cases where there are actual usages of ,raw in code:

  • src/mono/browser/debugger/BrowserDebugProxy/MonoSDBHelper.cs looks like it handles ,nq and ,raw modifiers in a same way, but I didn't have enough time to understand in which cases this helper is being used. Seems somehow related to mono.

  • src/Microsoft.Diagnostics.Monitoring.StartupHook/ParameterCapturing/ObjectFormatting/Formatters/DebuggerDisplay/DebuggerDisplayParser.cs This file in dotnet-monitor repo mentions ,nq and ,nse (NoSideEffects) modifiers. More than that tests for this class mentions ,raw modifiers but it seems that it doesn't handle it at all.

I had a feeling that answer may be in some open source debuggers

So I tried looking into netcoredbg and in mono project. netcoredbg didn't make use of any ,raw but surprisingly _mono used it lots of times. Again, I didn't find any notion of logic that handles that modifier somehow.

As a last resort I've tried to search some stuff in a visual studio installation:

I've found there xml documentation for Microsoft.CodeAnalysis.Scripting assembly, specifically - Hosting.CommonObjectFormatter.Visitor.FormatWithEmbeddedExpressions(System.Int32,System.String,System.Object), its documentation:

            <summary>
            Evaluate a format string with possible member references enclosed in braces. 
            E.g. "goo = {GetGooString(),nq}, bar = {Bar}".
            </summary>
            <remarks>
            Although in theory any expression is allowed to be embedded in the string such behavior is in practice fundamentally broken.
            The attribute doesn't specify what language (VB, C#, F#, etc.) to use to parse these expressions. Even if it did all languages 
            would need to be able to evaluate each other language's expressions, which is not viable and the Expression Evaluator doesn't 
            work that way today. Instead it evaluates the embedded expressions in the language of the current method frame. When consuming 
            VB objects from C#, for examale, the evaluation might fail due to language mismatch (evaluating VB expression using C# parser).
            
            Therefore we limit the expressions to a simple language independent syntax: {clr-member-name} '(' ')' ',nq', 
            where parentheses and ,nq suffix (no-quotes) are optional and the name is an arbitrary CLR field, property, or method name.
            We then resolve the member by name using case-sensitive lookup first with fallback to case insensitive and evaluate it.
            If parentheses are present we only look for methods.
            Only parameterless members are considered.
            </remarks>

That makes me think, that officially there is no support except for ,nq modifier, but I don't have enough proofs for that.

转载请注明原文地址:http://anycun.com/QandA/1744826263a88152.html