/// <summary> /// Locates a <see cref="NamedEvent"/> and unsubscribes a consumer /// </summary> /// <param name="subscriber">Identifier of the subscriber class</param> /// <param name="name">The name of the <see cref="NamedEvent"/> to unsubscribes from</param> /// <param name="handler">The <see cref="NamedEventHandler"/> containing the link to the consuming method</param> public void Unsubscribe(string subscriber, string name, NamedEventHandler handler) { NamedEvent subscription = subscriptions.FirstOrDefault(s => s.Name == name); if (subscription != null) { subscription.Subscribers.Remove(subscriber); subscription.Handler -= handler; } }
/// <summary> /// Locates or creates a <see cref="NamedEvent"/> and subscribes a consumer /// </summary> /// <param name="subscriber">Identifier of the subscriber class</param> /// <param name="name">The name of the <see cref="NamedEvent"/> to subscribe to</param> /// <param name="handler">The <see cref="NamedEventHandler"/> containing the link to the consuming method</param> public void Subscribe(string subscriber, string name, NamedEventHandler handler) { NamedEvent subscription = subscriptions.FirstOrDefault(s => s.Name == name); if (subscription != null) { if (!subscription.Subscribers.Contains(subscriber)) { subscription.Subscribers.Add(subscriber); } subscription.Handler -= handler; subscription.Handler += handler; } else { subscription = new NamedEvent(name, handler); subscription.Subscribers.Add(subscriber); subscriptions.Add(subscription); } }
/// <summary> /// Initializes a new instance of the <see cref="NamedEvent"/> class /// </summary> /// <param name="name">The name of the event</param> /// <param name="handler">A delegate containing a link to the consuming event method</param> public NamedEvent(string name, NamedEventHandler handler) { this.Name = name; this.Handler += handler; }