/// <summary> /// Raises the specified event. /// </summary> /// <param name="addAction">The action pointing to the add accessor.</param> /// <param name="removeAction">The action pointing to the remove accessor.</param> /// <param name="eventArgs">The <see cref="EventArgs"/> instance containing the event data.</param> /// <returns>The value returned from the event subscriber(s), if any.</returns> public object Raise(Action addAction, Action removeAction, EventArgs eventArgs) { EventAccessorPair accessorPair = _eventAccessorExtractor.FindEventAccessor(addAction, removeAction); object sender = accessorPair.TargetInstance; return(Raise(accessorPair, new[] { sender, eventArgs })); }
private object Raise(EventAccessorPair accessorPair, object[] arguments) { EventSurrogate surrogate; if (!_events.TryGetValue(accessorPair, out surrogate)) { throw new ArgumentException("Could not find event surrogate for add/remove accessor pair"); } return(surrogate.Raise(arguments.ToArray())); }
/// <summary> /// Intercepts the specified void event method. /// </summary> /// <typeparam name="TEventHandler">The type of the event handler.</typeparam> /// <param name="arguments">The arguments provided in the original method call.</param> /// <param name="invokedMethod">The invoked method.</param> /// <param name="addMethod">The add accessor method.</param> /// <param name="removeMethod">The remove accessor method.</param> /// <returns> /// A result that specifies whether the method was intercepted. /// </returns> private InterceptorResult InterceptVoidEventMethod <TEventHandler>(object[] arguments, MethodBase invokedMethod, MethodBase addMethod, MethodBase removeMethod) where TEventHandler : class { object targetInstance = invokedMethod.IsStatic ? null : arguments[0]; var key = new EventAccessorPair(targetInstance, addMethod, removeMethod); EventSurrogate surrogate = _events.GetOrAdd(key, _ => new EventSurrogate <TEventHandler>()); Delegate subscriber = (Delegate)(invokedMethod.IsStatic ? arguments[0] : arguments[1]); if (invokedMethod == addMethod) { surrogate.Subscribe(subscriber); } else { surrogate.Unsubscribe(subscriber); } // Invoke the original event accessors too. return(new InterceptorResult(false)); }
/// <summary> /// Raises the specified event. /// </summary> /// <param name="addAction">The action pointing to the add accessor.</param> /// <param name="removeAction">The action pointing to the remove accessor.</param> /// <param name="arguments">The arguments.</param> /// <returns> /// The value returned from the event subscriber(s), if any. /// </returns> public object Raise(Action addAction, Action removeAction, object[] arguments) { EventAccessorPair accessorPair = _eventAccessorExtractor.FindEventAccessor(addAction, removeAction); return(Raise(accessorPair, arguments)); }