调试自动属性

Mar*_*acz 122 c# properties breakpoints visual-studio

有没有办法在自动实现的属性中设置setter/getter的断点?

int Counter { get; set; }
Run Code Online (Sandbox Code Playgroud)

除了将其更改为标准属性(我这样做,但要做到这一点,我必须更改并重新编译整个项目)

Mat*_*ith 218

使用Visual Studio 2008,2010,2012,2013:

  1. 转到"断点"窗口
  2. 新功能 - >功能突破......
  3. 对于get,键入:ClassName.get_Counter()

    对于set,键入:ClassName.set_Counter(int)

当断点被击中时,你会得到一个"No Source Available",但是你将获得调用堆栈中的调用位置.

在MSDN上找到了这个解决方案

  • VS2015通过[在自动获取器和设置器上设置断点]来修复此问题(http://blogs.msdn.com/b/visualstudioalm/archive/2014/11/14/set-breakpoints-on-auto-implemented- properties-with-visual-studio-2015.aspx)_:)_ (11认同)
  • 这也适用于默认构造函数.使用`ClassName.ctor` (9认同)
  • 实际上`ClassName..ctor()`有效. (3认同)

and*_*lov 12

在 Visual Studio 2017 上:

将鼠标悬停在“设置”词上 -> 右键单击​​ -> 断点 -> 插入断点

前:

前

后:

后

  • 使用 VS2019 (3认同)

Kie*_*ron 7

如果我是你,我会暂时将该属性设置为内部字段支持的标准属性...设置断点,然后您可以将其更改回来.


Red*_*edX 7

这个问题很老,但值得一提的是它只适用于VS 2015.

https://blogs.msdn.microsoft.com/visualstudioalm/2014/11/14/set-breakpoints-on-auto-implemented-properties-with-visual-studio-2015/

class X {
  public string name {
    set;
    get; // setting a breakpoint here will break in VS 2015!
  }
}
Run Code Online (Sandbox Code Playgroud)