I have a MulticastDelegate
that can reference one of a number of (legacy) delegates that have the same signature. For example:
public delegate void ObjectCreated(object sender, EventArgs args);
public delegate void ObjectDeleted(object sender, EventArgs args);
//...
Those delegates are then used to define events:
public event ObjectCreated ObjectWasCreated;
public event ObjectDeleted ObjectWasDeleted;
I then have a method which takes in a MulticastDelegate
that I use to do some common checking:
void DispatchEvent(MulticastDelegate handler, object sender, EventArgs args)
{
if (handler != null)
{
// ...
handler.DynamicInvoke(sender, args);
}
}
Which is called from within other methods of the class wherein the events were defined:
DispatchEvent(ObjectWasCreated, sender, args);
DispatchEvent(ObjectWasDeleted, sender, args);
Is there a more concise way to do this that avoids DynamicInvoke?
EventArgs
but is using a custom subclass. However, I can't see any reason it shouldn't use the same delegate for each of the dispatched events -- then I can changed from MulticastDelegate to the delegate type in question. – Flogging