自动实现的属性设置器上的条件断点

本文关键字:条件断点 设置 属性 实现 | 更新日期: 2023-09-27 18:36:00

假设我在类名中有这个自动实现的属性:public int Counter{ get; set; }

我无法在 Visual Studio 2013 中的 C# 自动实现的属性资源库上成功设置条件断点。 具体来说,在设置的新值上。 (例如,当它设置为负数时,我想断点它。

我知道还有其他解决方案,例如分解属性,使其不是自动实现的属性,或者中断设置该属性的所有位置。但我希望能够在没有繁琐解决方法的情况下做到这一点。

我已经使用以下来自 https://stackoverflow.com/a/6713867/119418 的提示成功地在自动实现的属性设置器上断点

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

  1. 转到"断点"窗口
  2. 新功能>中断...
  3. 对于获取,键入:ClassName.get_Counter()

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

命中断点时,你将获得"无可用源",但你将在调用堆栈中获取调用位置。

自动实现的属性设置器上的条件断点

如果我正确的话,我不是 100% 确定您要做什么,如果有人将其设置为负值,您只想为属性设置断点

我可能在这里朝着错误的方向前进,但你在寻找这种东西吗

private int _age;
public int Age
{
      get{ return _age;  }
      set{ 
        if(value < 0) 
        { throw somthing;} //add a breakpoint here 
        else{ _age = value;} 
         }
}// im writing this directly in the browser so forgive space indentation etc 

这可能不完全是你想要的,但我确实认为这是实现你要求的最简单方法

不完全是问题的答案(对于Visual Studio 2013),但getter和setters的断点预计将在Visual Studio 2015中起作用。

https://devblogs.microsoft.com/devops/set-breakpoints-on-auto-implemented-properties-with-visual-studio-2015/