Пример #1
0
        /// <summary>
        ///     Notifies listener about an event.
        /// </summary>
        /// <param name="target">The specified listener to notify.</param>
        /// <param name="sender">The object that raised the event.</param>
        /// <param name="message">The message instance.</param>
        public static void Publish([NotNull] object target, [NotNull] object sender, [NotNull] object message)
        {
            Should.NotBeNull(target, "target");
            Should.NotBeNull(sender, "sender");
            Should.NotBeNull(message, "message");
            var handlerInfo = HandlerInfo.GetOrCreate(target);

            if (!handlerInfo.IsEmpty)
            {
                handlerInfo.Handle(sender, message, message is ITracebleMessage);
            }
            var customAction = PublishCustomAction;

            if (customAction != null)
            {
                customAction(target, sender, message);
            }
        }
Пример #2
0
        /// <summary>
        ///     Subscribes an instance to events.
        /// </summary>
        /// <param name="instance">The instance to subscribe for event publication.</param>
        public virtual bool Subscribe(object instance)
        {
            Should.NotBeNull(instance, "instance");
            var handlerInfo = HandlerInfo.GetOrCreate(instance);

            if (handlerInfo.IsEmpty)
            {
                return(false);
            }
            lock (_handlerReferences)
            {
                if (!Contains(instance, false))
                {
                    _handlerReferences.Add(ServiceProvider.WeakReferenceFactory(handlerInfo, true));
                }
            }
            return(true);
        }