views:

96

answers:

5

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?

A: 

You can use System.Diagnoistic.Debugger.Break() method for its purpose as follows

if(x!= y)
    Debugger.Break()
Nimesh
+2  A: 

Right click the red dot that represents the breakpoint - choose Condition and enter expression when you want the breakpoint to break the execution, or check Has changed to break whenever the value changes.

veljkoz
+3  A: 

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.

Timwi
A: 

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.

JaredPar
A: 

I don't think that there is a way in VS IDE that allows you to set a general breakpoint like that. If you really need this and willing to do some code changes, you can take a look at partial methods. Using partial methods, you define a partial method and call it from your setters. When you want to debug to find out who's calling a setter, you can provide a dummy implementation of the partial method. When you are done debugging, you can get rid of the implementation and the compiler will not generate anything related to that method as if it did not exist. This pollutes your code unless you can think of other potential uses of a partial method call from your setters. However, I just wanted to throw it out there so that you know.

Mehmet Aras