While debugging in Visual Studio 2013, I'd like to know the number of subscribers to the event PropertyChanged
published by a certain class (let's call it Publisher
) that implements INotifyPropertyChanged
.
I've researched a bit and found that this should be possible calling GetInvocationList()
and counting the elements in the returned array. So I've set a breakpoint in my code and tried to call this on an object called publisher
of class Publisher
in the Immediate window:
publisher.PropertyChanged.GetInvocationList()
However, I get this error message:
The event 'System.ComponentModel.INotifyPropertyChanged.PropertyChanged' can only appear on the left hand side of += or -=
What's wrong with what I've done?
How can I access the subscribers to PropertyChanged
?
How can I access the subscribers to
PropertyChanged
?
You don't, basically. An event only supports subscribe and unsubscribe functionality.
I've researched a bit and found that this should be possible calling GetInvocationList() and counting the elements in the returned array.
That assumes you can get at the underlying delegate field - if there even is one. There might not be - there are lots of ways an event can be implemented, just like there are lots of ways a property can be implemented.
Basically, what you're asking for breaks the encapsulation model of events. While there are ways around this in certain situations using reflection, you should be aware that you're fighting the design of the system.
See my article on events and delegates for more about the differences between the two.
See more on this question at Stackoverflow