Is it possible in Visual Studio 2010 to break on anything (a method for example) that changes an object's property?
Or is it possible to find out if object properties changed in an ASP.NET Webforms application?
Update:
Correct answer can be found at: http://stackoverflow.com/questions/2682613/cant-set-breakpoints-on-an-auto-property-setter-why/6713920#6713920
Setting a data breakpoint is as easy as right-clicking on the property you're interested in watching inside the watch, autos, or locals window and selecting “Break when value changes” in the context menu. All data breakpoints are displayed in the Breakpoints window.
Right-click the breakpoint symbol and select Conditions (or press Alt + F9, C). Or hover over the breakpoint symbol, select the Settings icon, and then select Conditions in the Breakpoint Settings window.
Breakpoints are the most basic and essential feature of reliable debugging. A breakpoint indicates where Visual Studio should suspend your running code so you can take a look at the values of variables, or the behavior of memory, or whether or not a branch of code is getting run.
An inline breakpoint can be set using Shift+F9 or through the context menu during a debug session. Inline breakpoints are shown inline in the editor. Inline breakpoints can also have conditions. Editing multiple breakpoints on a line is possible through the context menu in the editor's left margin.
If you have control over the code that declares the property, then certainly you can put a breakpoint inside the setter. Even if it is currently an auto-implemented property, e.g.:
public string SomeProperty { get; set; }
you can easily change it like this:
private string _someProperty;
public string SomeProperty {
get { return _someProperty; }
set {
// Set breakpoint here, or type Debugger.Break();
_someProperty = value;
}
}
If the value is actually a field instead of a property, you can still change it into a property to achieve the same thing.
If you don’t have access to the code that declares the property, then it’s quite a bit harder. Personally what I do is this, but it’s a bit laborious:
Declare a public static field in your Program
class of the type that declares the property.
Early in the program, find a reference to the object whose property value changes and put that reference in this static field. If necessary, use Reflection to retrieve private fields.
Add global::MyNamespace.Program.MyField.ImportantProperty
to the Watch window while debugging.
Step through the code until the value in the watch window changes.
It sounds like you're asking for a feature that would cause Visual Studio to break whenever a property / field is changed on an object.
For properties you can do this with normal breakpoints and a conditional. Simply set the breakpoint on the property setter, right click and select conditional. Then add a simple check to see if the new value and existing value are different
value != backingField
For fields there really is no good solution. The C++ debugger has a feature called Data BreakPoints which have exactly this behavior. But they are not available for managed languages, primarily because the CLR debugger does not support them. The best you can do is temporarily change your field to a property and use the above method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With