[英]Set breakpoint in Visual Studio when a member variable is accessed C#
在Visual Studio中为C#代码访问成员变量数据时,是否可以设置断点?
You can't achieve this for a regular field, because the debugger can not break on a simple memory copy operation. 对于常规字段,您无法实现此目的,因为调试器无法在简单的内存复制操作上中断。 However, let's say we have a field called
UncleName
, like so: 但是,假设我们有一个名为
UncleName
的字段,如下所示:
public static string UnclesName = "Bob";
You could turn it into a property: 您可以将其变成一个属性:
public static string _unclesName = "Bob";
public static string UnclesName { get { return _unclesName; } set { _unclesName = value; } }
What really happens, is the compiler generates two methods for you under the hood, one to get the data from the field, and one to set it. 真正发生的是,编译器在后台为您生成了两种方法,一种是从现场获取数据,另一种是进行设置 。
The debugger can can break on methods, and since we have now changed our data to be accessed through a method, namely the UnclesName.get()
method, we can insert a breakpoint on the get
keyword of our property, and have the debugger break every time the data is accessed. 调试器可以中断方法,并且由于我们现在已经更改了要通过方法(即
UnclesName.get()
方法UnclesName.get()
访问的数据,因此可以在属性的get
关键字上插入一个断点,并使调试器中断每次访问数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.