简体   繁体   English

如何在Visual Studio调试器中查看C#自动属性的支持字段?

[英]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. Visual Studio的调试器有时会进入无法计算表达式的状态。 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 . 例如,如果您正在查看堆栈顶部报告[Managed to Native Transition]的线程,并且您通过“本地”窗口中的this引用查看任何属性,则它们将报告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.) (属性变得不可用的原因是调试器实际上运行getter来检索值。如果您所在的线程被隐藏在某些非托管代码中,则无法执行此操作。)

Unfortunately, the compiler-generated fields that stored the value for C# auto properties don't show up in the debugger. 遗憾的是,存储C#auto属性值的编译器生成的字段不会显示在调试器中。 (These are the fields with names like <MyProperty>__BackingField .) (这些是名称如<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. 注意 :在这种情况下,重写代码以使用手动实现的属性不是一个选项,因为我想知道其值的属性是在Microsoft库中。 (Specifically, the Open XML SDK.) It's an auto property, and I can't change that. (具体来说,Open XML SDK。)这是一个自动属性,我无法改变它。

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. 还要注意 :简单地允许代码执行稍微进行以便它可以从本机代码转换返回不是一个选项,因为由于某种原因,代码已进入某种紧凑的繁忙循环 - 它消耗了CPU核心,并且永远不会回来 (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. 此外,如果它是相关的,我正在远程调试代码,因为它在Azure辅助角色上运行。 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. 据我所知,这排除了本机调试,所以我甚至不能去查看陷入循环的代码 - 我不得不通过查看托管来尝试推断它是什么在它消失进兔子洞之前就已经运行的代码。

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 ): 我相信以下监视表达式将起作用( cC类的一个实例):

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

Not very convenient, I know, but better than nothing. 我知道,不是很方便,但总比没有好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM