Beispiel #1
0
        /// <summary>
        /// 注册一个事件
        /// </summary>
        /// <param name="eventName">事件名称</param>
        /// <param name="handler">事件句柄</param>
        /// <param name="life">在几次后事件会被自动释放</param>
        /// <returns>事件句柄</returns>
        public IEventHandler Listen(string eventName, Func <object, object> handler, int life = 0)
        {
            Guard.Requires <ArgumentNullException>(eventName != null);
            Guard.Requires <ArgumentNullException>(handler != null);

            eventName = Normalize(eventName);

            var wildcard     = eventName.IndexOf("*") != -1;
            var eventHandler = new EventHandler(this, wildcard ? Str.AsteriskWildcard(eventName) : eventName, handler, life);

            lock (syncRoot)
            {
                if (wildcard)
                {
                    SetWildcardListener(eventHandler);
                }
                else
                {
                    List <EventHandler> handlers;
                    if (!this.handlers.TryGetValue(eventName, out handlers))
                    {
                        this.handlers[eventName] = handlers = new List <EventHandler>();
                    }
                    handlers.Add(eventHandler);
                }

                return(eventHandler);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 设定通配符事件
        /// </summary>
        /// <param name="eventName">事件名</param>
        /// <param name="execution">事件调用方法</param>
        /// <param name="group">事件分组</param>
        /// <returns>监听事件</returns>
        private IEvent SetupWildcardListen(string eventName, Func <string, object[], object> execution, object group)
        {
            if (!wildcardListeners.TryGetValue(eventName, out KeyValuePair <Regex, List <IEvent> > listener))
            {
                wildcardListeners[eventName] = listener =
                    new KeyValuePair <Regex, List <IEvent> >(new Regex(Str.AsteriskWildcard(eventName)), new List <IEvent>());
            }

            var output = MakeEvent(eventName, execution, group, true);

            listener.Value.Add(output);
            return(output);
        }
Beispiel #3
0
        /// <summary>
        /// 设定通配符事件
        /// </summary>
        /// <param name="eventName">事件名</param>
        /// <param name="target">事件调用目标</param>
        /// <param name="method">事件调用方法</param>
        /// <returns>监听事件</returns>
        private IEvent SetupWildcardListen(string eventName, object target, MethodInfo method)
        {
            KeyValuePair <Regex, List <IEvent> > listener;

            if (!wildcardListeners.TryGetValue(eventName, out listener))
            {
                wildcardListeners[eventName] = listener =
                    new KeyValuePair <Regex, List <IEvent> >(new Regex(Str.AsteriskWildcard(eventName)), new List <IEvent>());
            }

            var output = MakeEvent(eventName, target, method, true);

            listener.Value.Add(output);
            return(output);
        }