简体   繁体   中英

Conditional breakpoint on auto-implemented property setter

Say I have this auto-implemented property in class ClassName: public int Counter{ get; set; } public int Counter{ get; set; }

I have not successfully been able to have a conditional breakpoint on a C# auto-implemented property setter in Visual Studio 2013. Specifically, on the new value being set. (I would like to breakpoint it when it is set to a negative number, for example.)

I know there are other solutions, like breaking out the property so that it isn't an auto-implemented property, or breakpointing all places that set that property. But I would love to just be able to do it without tedious workarounds.

I have successfully breakpointed on an auto-implemented property setter using the following tip from https://stackoverflow.com/a/6713867/119418

Using Visual Studio 2008, 2010, 2012, 2013:

  1. Go to the Breakpoint window
  2. New->Break at Function…
  3. For the get, type: ClassName.get_Counter()

    For the set, type: ClassName.set_Counter(int)

You'll get a "No Source Available" when the breakpoint is hit, but you'll get the calling location in the call stack.

Not exactly the answer to the question (for Visual Studio 2013), but breakpointing on getters and setters is expected to work in Visual Studio 2015.

http://blogs.msdn.com/b/visualstudioalm/archive/2014/11/14/set-breakpoints-on-auto-implemented-properties-with-visual-studio-2015.aspx

im not 100% sure what your trying to do if i got this correct you just want to set a breakpoint to a property if someone sets it to a negative value

i could be be heading in the wrong direction here but are you looking for this kind of thing

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 

this may not be exactly what you want but i do see it as the easiest way to achieve what your asking

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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