Consider the code using GetInvocationList
:
foreach (var handler in theEvent.GetInvocationList().Cast<TheEventHandler>()) {
// handler is then of the TheEventHandler type
try {
handler(sender, ...);
} catch (Exception ex) {
// uck
}
}
This my old approach, the newer approach I prefer is above because it makes invocation a snap, including the use of out/ref parameters (if desired).
foreach (var singleDelegate in theEvent.GetInvocationList()) {
try {
singleDelgate.DynamicInvoke(new object[] { sender, eventArg });
} catch (Exception ex) {
// uck
}
}
which individually calls each delegate that would have been invoked with
theEvent.Invoke(sender, eventArg)
Happy coding.
Remember to do the standard null-guard copy'n'check (and perhaps lock) when dealing with events.