/// <summary> /// Removes child from list of events. /// </summary> /// <param name="item">Child to remove.</param> public void InvalidateChildReference(SubscriptionEventInfo item) { Events.Remove(item); item = null; if (Events.Count == 0) { InvalidateReference(); } }
/// <summary> /// Initializes a new instance of the <see cref="SubscriberInfo"/> class. /// </summary> /// <param name="parent">Parent container.</param> /// <param name="subscriber">Object receiving notifications.</param> /// <param name="subscriberType">Type of object receiving notifications.</param> /// <param name="eventHandler">Method that will be invoked to handle notification.</param> public SubscriberInfo(SubscriptionEventInfo parent, object subscriber, Type subscriberType, EventHandler eventHandler) { if (object.ReferenceEquals(null, subscriber)) { throw new ArgumentNullException(nameof(subscriber)); } _subscriberReference = new WeakReference(subscriber); SubscriberType = subscriberType; EventHandlerMethodName = null; _parent = parent; Handler = eventHandler; }
/// <summary> /// Creates a new subscription. If a subscription with the same parameters already exists, nothing happens. /// </summary> /// <typeparam name="TSubscribeTo">Type of object being subscribed to, that will transmit event.</typeparam> /// <typeparam name="TSubscriber">Type of object receiving notifications.</typeparam> /// <param name="subscribeToEvent">Name of the event to subscribe to.</param> /// <param name="subscriber">Object receiving notifications.</param> /// <param name="eventHandler">Method that will be invoked to handle notification.</param> public static void Subscribe <TSubscribeTo, TSubscriber>(string subscribeToEvent, TSubscriber subscriber, EventHandler eventHandler) { SubscriptionContainerInfo newSubscriptionContainer = null; SubscriptionEventInfo newEventInfo = null; SubscriberInfo newSubscriber = null; var subscribeToContainerType = typeof(TSubscribeTo); var subscriberType = typeof(TSubscriber); var currentSubscriptions = _subscriptions.Where(x => x.SubscribeToContainerType == subscribeToContainerType); foreach (var subscriptionContainer in currentSubscriptions) { var eventInfos = subscriptionContainer.Events.Where(x => x.EventName == subscribeToEvent); foreach (var eventInfo in eventInfos) { var existingSubscriber = eventInfo .Subscribers .Where(x => x.SubscriberType == subscriberType && x.Handler == eventHandler) .FirstOrDefault(); if (!object.ReferenceEquals(null, existingSubscriber)) { return; } newSubscriber = new SubscriberInfo(eventInfo, subscriber, subscriberType, eventHandler); eventInfo.Subscribers.Add(newSubscriber); return; } // else, not found in Events. newEventInfo = new SubscriptionEventInfo(subscriptionContainer, subscribeToEvent); newSubscriber = new SubscriberInfo(newEventInfo, subscriber, subscriberType, eventHandler); newEventInfo.Subscribers.Add(newSubscriber); subscriptionContainer.Events.Add(newEventInfo); return; } // else, not found in current subscriptions newSubscriptionContainer = new SubscriptionContainerInfo(subscribeToContainerType); newEventInfo = new SubscriptionEventInfo(newSubscriptionContainer, subscribeToEvent); newSubscriber = new SubscriberInfo(newEventInfo, subscriber, subscriberType, eventHandler); newEventInfo.Subscribers.Add(newSubscriber); newSubscriptionContainer.Events.Add(newEventInfo); _subscriptions.Add(newSubscriptionContainer); }
/// <summary> /// Initializes a new instance of the <see cref="SubscriberInfo"/> class. /// </summary> /// <param name="parent">Parent container.</param> /// <param name="subscriber">Object receiving notifications.</param> /// <param name="subscriberType">Type of object receiving notifications.</param> /// <param name="eventHandlerMethodName">Name of method that will be invoked via reflection to handle notification.</param> public SubscriberInfo(SubscriptionEventInfo parent, object subscriber, Type subscriberType, string eventHandlerMethodName) { if (object.ReferenceEquals(null, subscriber)) { throw new ArgumentNullException(nameof(subscriber)); } _subscriberReference = new WeakReference(subscriber); SubscriberType = subscriberType; EventHandlerMethodName = eventHandlerMethodName; Handler = null; _parent = parent; _methodInfo = SubscriberType.GetMethod(EventHandlerMethodName); if (object.ReferenceEquals(null, _methodInfo)) { throw new ArgumentException($"Could not find {eventHandlerMethodName} on type {subscriberType.FullName}"); } }