Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2010 - Break on anything that changes an object property

Tags:

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

like image 479
Rookian Avatar asked Aug 30 '10 12:08

Rookian


People also ask

How do you break a variable change in Visual Studio?

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.

How do I add a conditional breakpoint in Visual Studio?

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.

What is break point in Visual Studio?

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.

How do you put a breakpoint in VS code?

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.


2 Answers

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:

  1. Declare a public static field in your Program class of the type that declares the property.

  2. 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.

  3. Add global::MyNamespace.Program.MyField.ImportantProperty to the Watch window while debugging.

  4. Step through the code until the value in the watch window changes.

like image 50
Timwi Avatar answered Oct 14 '22 07:10

Timwi


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.

like image 35
JaredPar Avatar answered Oct 14 '22 08:10

JaredPar