示例#1
0
 /// <summary>
 /// Registers the event substriber.
 /// </summary>
 /// <param name="model">The model.</param>
 private void RegisterEventSubstriber(ComponentModel model)
 {
     MethodInfo[] methods = model.Implementation.GetMethods();
     foreach (MethodInfo methodInfo in methods)
     {
         object[] attrs = methodInfo.GetCustomAttributes(typeof(EventSubscriberAttribute), true);
         if (attrs.Length > 0)
         {
             EventSubscriberAttribute subscriberAttr = (EventSubscriberAttribute)attrs[0];
             IEventDispatcher         dispatcher     = Kernel[typeof(IEventDispatcher)] as IEventDispatcher;
             dispatcher.RegisterEventSubscriber(subscriberAttr.Topic, methodInfo, model.Service);
         }
     }
 }
示例#2
0
        /// <summary>
        /// Register event handlers in the handler instance.
        ///
        /// One handler instance may have many event handler methods.
        /// These methods have EventSubscriberAttribute contract and exactly one parameter.
        /// </summary>
        /// <remarks>
        /// <para>The handler may be invoked out of the event delivery order under heavy load.</para>
        /// <para>If you need the event processed in the delivery order, use OrderedEventBus instead.</para>
        /// </remarks>
        /// <param name="handler">The instance of event handler class</param>
        public void Register(object handler)
        {
            if (handler == null)
            {
                return;
            }

            MethodInfo[] miList = handler.GetType().GetMethods();
            lock (_eventHandlerLock)
            {
                // Don't allow register multiple times.
                if (_eventHandlerList.Any(record => record.Handler == handler))
                {
                    return;
                }

                List <EventHandlerHolder> newList = null;
                foreach (MethodInfo mi in miList)
                {
                    EventSubscriberAttribute attribute = mi.GetCustomAttribute <EventSubscriberAttribute>();
                    if (attribute != null)
                    {
                        ParameterInfo[] piList = mi.GetParameters();
                        if (piList.Length == 1)
                        {
                            // OK, we got valid handler, create newList as needed
                            if (newList == null)
                            {
                                newList = new List <EventHandlerHolder>(_eventHandlerList);
                            }
                            newList.Add(new EventHandlerHolder(handler, mi, piList[0].ParameterType));
                        }
                    }
                }

                // OK, we have new handler registered
                if (newList != null)
                {
                    _eventHandlerList = newList;
                }
            }
        }
示例#3
0
 private void EventToServer(object component, bool isReigster)
 {
     //Program.Logger.Debug("检查组件[" + component.GetType().Name + "]的服务器订阅事件");
     MethodInfo[] methods = component.GetType().GetMethods();
     foreach (MethodInfo methodInfo in methods)
     {
         object[] attrs = methodInfo.GetCustomAttributes(typeof(EventSubscriberAttribute), true);
         if (attrs.Length > 0)
         {
             EventSubscriberAttribute subscriberAttr = (EventSubscriberAttribute)attrs[0];
             //Program.Logger.Debug("发现一个[" + subscriberAttr.Topic + "]事件的订阅方法[" + methodInfo.Name + "]");
             if (isReigster)
             {
                 //Program.Logger.Debug("向服务器注册事件");
                 ClientEventDispatcher.Instance.RegisterEventSubscriber(subscriberAttr.Topic, component, methodInfo);
             }
             else
             {
                 //Program.Logger.Debug("向服务器注销事件");
                 ClientEventDispatcher.Instance.UnRegisterEventSubscriber(subscriberAttr.Topic, methodInfo, component);
             }
         }
     }
 }