当托管语言中的数据更改时中断的断点

发布于 2024-09-25 06:24:14 字数 222 浏览 2 评论 0原文

我有一个带有列表属性的类,在某些情况下似乎会丢失一个元素。我不知道什么时候会发生这种情况。

所以我想要做的是设置一个 Visual Studio 断点,该断点将在该值更改时暂停程序。条件断点在这种情况下不起作用,因为我不知道是什么删除了这个断点。

换句话说,我希望我的程序在 myList.Count 计算出新数字时停止。

关于如何做到这一点有什么想法吗?

I have a class with a list property that seems to lose an element under certain circumstances. I cannot find out when this happens.

So what I'd like to do is set up a Visual Studio breakpoint that will pause the program the moment this value changes. A conditional breakpoint would not work in this scenario, since I have no idea what is removing this breakpoint.

To put it another way, I want my program to stop the moment myList.Count evaluates to a new number.

Any ideas on how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

淡紫姑娘! 2024-10-02 06:24:15

您可以在 Visual Studio 中设置数据断点,但这对于托管代码来说很难做到,因为垃圾收集器可能会移动对象。也就是说,你也许仍然能够成功。您必须为您的进程启用本机调试。在立即窗口中加载 SOS 并使用 !DumpObject 查找 Count 属性的后备存储地址。使用该地址,使用该地址创建一个新的数据断点,然后继续并触发问题。

You can set data breakpoints in visual studio but this is going to be difficult to do for managed code, as the garbage collector may move the object around. That said, you may still be able to pull it off. You will have to enable native debugging for your process. Load SOS in the immediate window and use !DumpObject to find the address of the backing store for the Count property. Using this address, create a new data breakpoint with this address and then continue and trigger the issue.

赴月观长安 2024-10-02 06:24:15

查找此特定属性的所有用法,并将断点添加到从此列表中删除元素的所有行。

或者您可以创建自己的 IList 实现并将断点设置为 Remove 方法(您不能在不更改所有客户端的情况下子类化 List,因为 List::Remove 不是虚拟的)。

Find all usages for this particular property and add breakpoint to all lines that removes elements from this list.

Or you may create your own IList implementation and set breakpoint to Remove method (you can't subclass List without changing all you clients, because List::Remove isn't virtual).

清风夜微凉 2024-10-02 06:24:15

这可能更像是一个问题而不是答案,但是如果您以这种方式设置 Visual Studio,则可以在调试时进入框架代码。然后您可以将断点放入实际的 List 实现中。

This is maybe more of a question than an answer, but you can step into Framework code when debugging, provided you set up your Visual studio that way. It could be that you can then put the breakpoint into the actual List implementation.

恋你朝朝暮暮 2024-10-02 06:24:15

这可能听起来太麻烦或复杂,但您可以使用计时器/后台线程来继续测试计数值,并在发现值与其先前实例不同时执行 Debugger.Break() 。

this may sound too out of the way or complex but can you use timer/background thread to keep testing the count value and do a Debugger.Break() whenever it finds the value different from its previous instance.

若水微香 2024-10-02 06:24:14

由于 CLR 限制,这在 C# 或任何其他 .NET 语言中是不可能的。 Visual Studio 本机代码调试器支持 C++ 代码的数据断点(链接)正是这样做的,但这不支持托管代码。您可以尝试中断或拦截集合上的 AddRemove 方法调用,如此问题的其他答案中所建议的。

This is not possible in C# or any of the other .NET languages due to CLR limitations. The Visual Studio native code debugger supports data breakpoints (link) for C++ code which do exactly this but this is not supported for managed code. You could try to break on or intercept Add and Remove method calls on the collection as suggested in the other answer to this question.

征﹌骨岁月お 2024-10-02 06:24:14

List 替换为 ObservableCollection 并侦听 CollectionChanged 事件怎么样?它实现了 IList 接口,因此可用方法中应该有足够的重叠,以实现语法和语义兼容性。

What about swapping out List<T> for ObservableCollection<T> and listen for the CollectionChanged event? It implements the IList<T> interface so there should be enough overlap in available methods to result in syntax and semantic compatibility.

子栖 2024-10-02 06:24:14

现在,这在 Visual Studio 2019 中成为可能。
请参阅此处的发行说明:https://learn.microsoft.com /en-us/visualstudio/releases/2019/release-notes

本文使用预览版 2 进行了一些详细介绍。
https://devblogs.microsoft.com/visualstudio/break-when-value-changes-data-breakpoints-for-net-core-in-visual-studio-2019/

请注意,这是针对 .NET 的仅限核心,而不是即将成为传统的、成熟的仅限 Windows 的 .NET 框架。

This is now possible in Visual Studio 2019.
See release notes here: https://learn.microsoft.com/en-us/visualstudio/releases/2019/release-notes

This article goes into some detail using Preview 2.
https://devblogs.microsoft.com/visualstudio/break-when-value-changes-data-breakpoints-for-net-core-in-visual-studio-2019/

Note that this is for .NET Core only and not the soon-to-be-legacy full fledged Windows only .NET framework.

明天过后 2024-10-02 06:24:14

我假设 Visual Studio 是 IDE。

设置断点,右键单击它,选择条件,键入 myList.Count,然后选择 Has Changed。

I'm assuming Visual Studio is IDE.

Set a breakpoint, right click it, select condition, type myList.Count, and choose Has Changed.

聽兲甴掵 2024-10-02 06:24:14

子类列表使用您自己的类,然后覆盖 Count(或添加/删除)并在您创建的方法中添加断点。

编辑:正如评论中提到的,这将需要大量的努力,因为添加和删除方法不是虚拟的;需要完全重写这些方法。

此外,子类化 Collection显然是一个更好的解决方案(尽管我无法辨别为什么因为添加/删除也不是 Collection的虚拟成员;评论?)。

Subclass List<t> with your own class, then override Count (or Add/Remove) and add a breakpoint in the method you create.

EDIT: As mentioned in comments, this would require a great deal of effort since the Add and Remove methods aren't virtual; a complete rewrite of the methods would be needed.

Also, subclassing Collection<t> would apparently be a better solution (though I can't discern a reason why since Add/Remove aren't virtual members for Collection<t> either; comments?).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文