コード例 #1
0
ファイル: Messenger.cs プロジェクト: BenEzard/MyPA
        /// <summary>
        /// Unregisters a messenger recipient with a matching context completely.
        /// After this method is executed, the recipient will no longer receive any messages.
        /// </summary>
        /// <param name="recipient"></param>
        /// <param name="context"></param>
        public void Unregister <T>(object recipient, object context)
        {
            object action;
            var    key = new MessengerKey(recipient, context, typeof(T));

            Dictionary.TryRemove(key, out action);
        }
コード例 #2
0
            /// <summary>
            /// Determines if the specified MessengerKey is the same to the current messengerkey
            /// </summary>
            /// <param name="_other"></param>
            /// <returns></returns>
            public bool Equals(MessengerKey _other)
            {
                if (ReferenceEquals(null, _other))
                {
                    return(false);
                }
                if (ReferenceEquals(this, _other))
                {
                    return(true);
                }


                return(Equals(Recipient, _other.Recipient) && Equals(Context, _other.Context) && Equals(Context, _other.Type));
            }
コード例 #3
0
        public void Unsubscribe(object subscriber, MessengerEvent msgEvent)
        {
            object       action;
            MessengerKey key = GetMessengerKey(subscriber, msgEvent);


            if (subscriber != null)
            {
                if (key != null)
                {
                    Dictionary.TryRemove(key, out action);
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Registers a recipient for a type of message T and a matching context. The action parameter will be executed
        /// when a corresponding message is sent.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="recipient"></param>
        /// <param name="action"></param>
        /// <param name="context"></param>
        public void Register <T>(object recipient, Action <T> action, object context)
        {
            var key = new MessengerKey(recipient, context);

            if (!Dictionary.TryAdd(key, action))
            {
                //Add Failed
                if (Dictionary.ContainsKey(key))
                {
                    throw new ArgumentException("Recipient '" + recipient + "' for context '" + context + "' is allready registered!");
                }
                throw new ArgumentException("Unable to add recipient.");
            }
        }
コード例 #5
0
            public override bool Equals(object obj)
            {
                if (ReferenceEquals(null, obj))
                {
                    return(false);
                }
                if (ReferenceEquals(this, obj))
                {
                    return(true);
                }
                if (obj.GetType() != GetType())
                {
                    return(false);
                }

                MessengerKey messageKey = obj as MessengerKey;

                return(this.Key.Equals(messageKey.Key) && this.Recipient.Equals(messageKey.Recipient));
            }
コード例 #6
0
        private MessengerKey GetMessengerKey(object subscriber, MessengerEvent msgEvent)
        {
            MessengerKey item = null;

            try
            {
                foreach (var key in Dictionary.Keys)
                {
                    if (subscriber == key.Subscriber && msgEvent == (MessengerEvent)key.Context)
                    {
                        item = key;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return(item);
        }
コード例 #7
0
        /// <summary>
        /// Registers a recipient for a type of message T and a matching context.
        /// The action parameter will be executed when a corresponding message is send
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_recipient"></param>
        /// <param name="_action"></param>
        /// <param name="_context"></param>
        public void Register <T>(object _recipient, Action <T> _action, object _context)
        {
            var key = new MessengerKey(_recipient, _context, typeof(T));

            dictionary.TryAdd(key, _action);
        }
コード例 #8
0
        /// <summary>
        /// Unregisters a messenger recipient with a matching context completely. After this method is executed, the recipient will
        /// no longer receive any messages.
        /// </summary>
        /// <param name="recipient"></param>
        /// <param name="context"></param>
        public void Unregister(object recipient, object context)
        {
            var key = new MessengerKey(recipient, context);

            _dictionary.TryRemove(key, out var action);
        }
コード例 #9
0
        public void Subscribe <T>(object subscriber, MessengerEvent mgsEvent, Action <T> handler)
        {
            var key = new MessengerKey(subscriber, mgsEvent, Dictionary.Count);

            Dictionary.TryAdd(key, handler);
        }
コード例 #10
0
 protected bool Equals(MessengerKey key)
 {
     return(Equals(Subscriber, key.Subscriber) && Equals(Context, key.Context));
 }