static Boolean InvokeAllEventHandlers <TArgs>(this GenericEventHandler <TArgs> evt, TArgs args, out Exception[] occurredExceptions) { LinkedList <Exception> exceptions = null; var result = evt != null; if (result) { foreach (var handler in evt.GetInvocationList()) { try { ((GenericEventHandler <TArgs>)handler)?.Invoke(args); } catch (Exception exc) { if (exceptions == null) { exceptions = new LinkedList <Exception>(); } exceptions.AddLast(exc); } } } if (exceptions != null) { occurredExceptions = exceptions.ToArray(); } else { occurredExceptions = Empty <Exception> .Array; } return(result); }
static Boolean InvokeAllEventHandlers <TArgs>(this GenericEventHandler <TArgs> evt, TArgs args, Boolean throwExceptions = true) { LinkedList <Exception> exceptions = null; var result = evt != null; if (result) { var invocationList = evt.GetInvocationList(); for (var i = 0; i < invocationList.Length; ++i) { try { ((GenericEventHandler <TArgs>)invocationList[i])?.Invoke(args); } catch (Exception exc) { if (throwExceptions) { if (exceptions == null) { // Just re-throw if this is last handler and first exception if (i == invocationList.Length - 1) { throw; } else { exceptions = new LinkedList <Exception>(); } } exceptions.AddLast(exc); } } } } if (exceptions != null) { throw new AggregateException(exceptions.ToArray()); } return(result); }