I have one event in C#. There are five subscribers for that. All the subscribers are different classes. But while raising the event i want that not all the subscriber/handler should be notified to handle this event. I should have some filtering mechanism and then only remaining subscribers should be notified. What could be the best way to achieve this?
If you want to do it with your existing even then just iterate through the invocation list on the event.
var list = localHandler.GetInvocationList();
foreach (EventHandler<T> item in list)
{
if(((ICanDoThing)item.Target).CanDoThing)
{
item(this, someArgs);
}
}
Now, you can see I've cast item.Target to a type of ICanDoThing, which is an interface I've just made up that exposes a method "CanDoThing". This allows you to query the object for whether it supports your particular need.
You should probably question whether you should use an event anyway for this, but the above will allow you to do so.
You could use the observer pattern, described here: http://www.dofactory.com/Patterns/PatternObserver.aspx and implement your logic in the update method of the observer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With