开发者

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

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/questio开发者_如何学JAVAns/2682613/cant-set-breakpoints-on-an-auto-property-setter-why/6713920#6713920


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.


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.


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.


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.


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

if(x!= y)
    Debugger.Break()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜