c# - 如何访问 PropertyChanged 事件的订阅者?

标签 c# .net visual-studio events visual-studio-2013

<分区>

在 Visual Studio 2013 中进行调试时,我想知道某个类(我们称它为 Publisher)发布的事件 PropertyChanged 的订阅者数量实现 INotifyPropertyChanged

我研究了一下,发现这应该可以调用 GetInvocationList() 并计算返回数组中的元素。因此,我在我的代码中设置了一个断点,并尝试在立即窗口中的 Publisher 类的名为 publisher 的对象上调用它:

publisher.PropertyChanged.GetInvocationList()

但是,我收到此错误消息:

The event 'System.ComponentModel.INotifyPropertyChanged.PropertyChanged' can only appear on the left hand side of += or -=

我做的有什么问题吗? 如何访问 PropertyChanged 的订阅者?

最佳答案

How can I access the subscribers to PropertyChanged?

基本上,您不需要。事件支持订阅和取消订阅功能。

I've researched a bit and found that this should be possible calling GetInvocationList() and counting the elements in the returned array.

这假设您可以获取底层的委托(delegate)字段 - 如果有 的话。可能没有 - 可以通过多种方式实现事件,就像可以通过多种方式实现属性一样。

基本上,您的要求打破了事件的封装模型。虽然在某些情况下使用反射可以解决此问题,但您应该意识到您正在与系统设计作斗争。

请参阅我关于 events and delegates 的文章详细了解两者之间的差异。

关于c# - 如何访问 PropertyChanged 事件的订阅者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20682476/

相关文章:

.net - Richter 在描述非虚拟方法调用的内部结构时是否弄错了?

css - Visual Studio 查找 CSS 类的用法

c# - 具有 int 值的枚举?

c# - 哪种调用 Form.ShowDialog() 的方法更好?

c# - 弱引用和事件处理

c# - Visual Studio/ReSharper 代码格式化 - C#

visual-studio - 如何在 Visual Studio 2010 中设置相对包含路径?

c# - 如何在 C# Winforms 应用程序中创建这样的多列 TreeView ?

c# uint 到 ushort 溢出,就像在 native C 中一样

c# - 获取 OAuth2 刷新 token