Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view backing fields for C# auto properties in Visual Studio debugger?

Visual Studio's debugger sometimes gets into a state where it is unable to evaluate expressions. This prevents it from displaying property values. For example, if you're looking at a thread where the top of the stack reports [Managed to Native Transition], and you look at any properties through, say, the this reference in the Locals window, they will all report Cannot evaluate expression because a native frame is on top of the call stack.

You can still inspect fields when the debugger is in this state, because that does not require the ability to execute code. (The reason properties become unavailable is that the debugger actually runs the getter to retrieve the value. It can't do that if the thread you're on is buried in some unmanaged code.)

Unfortunately, the compiler-generated fields that stored the value for C# auto properties don't show up in the debugger. (These are the fields with names like <MyProperty>__BackingField.)

I've tried enabling the "Show raw structure of objects in variables window" setting in the debugger options, but that doesn't appear to help with these hidden fields.

Is there some way to get the debugger to show me fields that the compiler has hidden?

Or alternatively, is there some other way I can discover the value of an auto property's backing field when the usual property evaluation is unavailable?


Note: rewriting the code to use a manually implemented property isn't an option in this case, because the property whose value I want to know is in a Microsoft library. (Specifically, the Open XML SDK.) It's an auto property, and I can't change that.

Also note: simply allowing the code execution to proceed a little so that it can return from the native code transition isn't an option because for some reason, the code has entered some sort of tight busy loop - it's consuming a CPU core, and never returns. (I'm trying to diagnose that problem which is why I'm trying to find out what the object's property values are - I'm trying to get an accurate picture of how it gets into this state.)

One more note: this is not a duplicate of Acessing the backing field in an auto property - my question is very specifically about reading the value while debugging.

Also, in case it's relevant, I'm debugging the code remotely as it runs on an Azure worker role. The problem only occurs in that environment. This rules out native debugging as far as I can tell, so I can't even go and look at the bit of code that's stuck in a loop - I'm having to try an infer what it was up to by looking at the managed code that was running immediately before it disappeared off into that rabbit hole.

like image 221
Ian Griffiths Avatar asked Nov 07 '13 16:11

Ian Griffiths


1 Answers

Say you have a class with a property you want to watch in a debugger:

public class C
{
    // ...
    public int I { get; set; }
    // ...
}

I believe the following watch expression will work (c is an instance of class C):

c.GetType().GetField("<I>k__BackingField",
    BindingFlags.Instance | BindingFlags.NonPublic).GetValue(c)

Not very convenient, I know, but better than nothing.

like image 57
Igor Korkhov Avatar answered Nov 15 '22 07:11

Igor Korkhov