visual-studio Visual Studio 2010 - 打破任何改变对象属性的东西

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3600186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:24:05  来源:igfitidea点击:

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

c#visual-studiodebuggingvisual-studio-2010

提问by Rookian

Is it possible in Visual Studio 2010 to break on anything (a method for example) that changes an object's property?

在 Visual Studio 2010 中是否有可能破坏更改对象属性的任何内容(例如方法)?

Or is it possible to find out if object properties changed in an ASP.NET Webforms application?

或者是否可以查明 ASP.NET Webforms 应用程序中的对象属性是否发生了变化?



Update:

更新:

Correct answer can be found at: http://stackoverflow.com/questions/2682613/cant-set-breakpoints-on-an-auto-property-setter-why/6713920#6713920

正确答案可以在:http: //stackoverflow.com/questions/2682613/cant-set-breakpoints-on-an-auto-property-setter-why/6713920#6713920

采纳答案by Timwi

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

如果您可以控制声明属性的代码,那么您当然可以在 setter 中放置一个断点。即使它当前是自动实现的属性,例如:

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 fieldinstead 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 Programclass 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.ImportantPropertyto the Watch window while debugging.

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

  1. Program声明属性的类型的类中声明一个公共静态字段。

  2. 在程序的早期,找到对其属性值发生变化的对象的引用,并将该引用放入此静态字段中。如有必要,请使用反射来检索私有字段。

  3. global::MyNamespace.Program.MyField.ImportantProperty调试时添加到监视窗口。

  4. 逐步执行代码,直到观察窗口中的值发生变化。

回答by JaredPar

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.

听起来您要求的功能会在对象上的属性/字段发生更改时导致 Visual Studio 中断。

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.

对于字段,确实没有好的解决方案。C++ 调试器有一个称为 Data BreakPoints 的特性,它具有这种行为。但是它们不适用于托管语言,主要是因为 CLR 调试器不支持它们。您能做的最好的事情是暂时将您的字段更改为属性并使用上述方法。

回答by veljkoz

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

右键单击代表断点的红点 -Condition当您希望断点中断执行时选择并输入表达式,或者Has changed在值更改时检查以中断。

回答by Mehmet Aras

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.

我认为 VS IDE 中没有一种方法可以让您设置这样的通用断点。如果你真的需要这个并且愿意做一些代码更改,你可以看看partial methods。使用分部方法,您可以定义分部方法并从您的 setter 中调用它。当您想调试以找出谁在调用 setter 时,您可以提供部分方法的虚拟实现。完成调试后,您可以摆脱实现,编译器不会生成与该方法相关的任何内容,就好像它不存在一样。这会污染您的代码,除非您能想到来自 setter 的部分方法调用的其他潜在用途。但是,我只是想把它扔到那里让你知道。

回答by DotNetInfo

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

您可以使用 System.Diagnoistic.Debugger.Break() 方法,如下所示

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