public static object OnEventListenerAdded(object instance, Type type, string eventName, object[] args) { var arguments = new EventExecutionArguments(); var evt = type?.GetEvent(eventName); arguments.executionType = EventExecutionType.AddListener; arguments.eventObject = evt; arguments.arguments = args; var method = type.GetMethod("add_" + eventName + APPENDIX); // Get start aspects and process them var startAspects = evt.GetCustomAttributes(typeof(IEventBeforeListenerAddedAspect), true); foreach (var aspect in startAspects) { ((IEventBeforeListenerAddedAspect)aspect).BeforeEventListenerAdded(arguments); } if (!arguments.HasErrored) { try { method.Invoke(instance, args); } catch (Exception ex) { arguments.exception = ex.InnerException; } } // Get start aspects and process them var exitAspects = evt.GetCustomAttributes(typeof(IEventAfterListenerAddedAspect), true); foreach (var aspect in exitAspects) { ((IEventAfterListenerAddedAspect)aspect).AfterEventListenerAdded(arguments); } // If had error throw it again after processing if (arguments.HasErrored) { // Invoke event if exists arguments.onException?.Invoke(arguments.exception); // Invoke exception aspects var exceptionAspects = evt.GetCustomAttributes(typeof(IEventExceptionThrownAspect), true); foreach (var aspect in exceptionAspects) { ((IEventExceptionThrownAspect)aspect).OnExceptionThrown(arguments.exception, arguments); } // Rethrow exception // ReSharper disable once PossibleNullReferenceException, already checked using HasErrored throw arguments.exception; } // Return value return(null); }
public static void AfterEventInvoked(object instance, Type type, string eventName) { var arguments = new EventExecutionArguments(); var evt = type?.GetEvent(eventName); arguments.executionType = EventExecutionType.Invoke; arguments.eventObject = evt; arguments.arguments = null; var startAspects = evt.GetCustomAttributes(typeof(IAfterEventInvokedAspect), true); foreach (var aspect in startAspects) { ((IAfterEventInvokedAspect)aspect).BeforeEventInvoked(arguments); } }