示例#1
0
        /// <summary>
        /// 取消注册事件
        /// </summary>
        /// <param name="handlerType"></param>
        /// <returns></returns>
        public bool UnregisterHandler(Type handlerType)
        {
            if (handlerType == null || handlerType.IsInterface)
            {
                return(false);
            }
            var exportAttributes = handlerType.GetCustomAttributes(typeof(EventExportAttribute), false);

            if (exportAttributes == null || exportAttributes.Length == 0)
            {
                return(false);
            }

            EventExportAttribute exportAttribute = exportAttributes[0] as EventExportAttribute;

            if (exportAttribute == null)
            {
                return(false);
            }
            var eventType = exportAttribute.InterfaceType.GetGenericArguments()[0];

            if (eventType == null)
            {
                return(false);
            }

            if (eventHandlerContainer.ContainsKey(eventType))
            {
                var deleteEventlist = eventHandlerContainer[eventType].Where(pre => pre.ImplementType == handlerType || pre.InterfaceType == handlerType)
                                      .ToList();

                for (int i = 0; i < deleteEventlist.Count; i++)
                {
                    eventHandlerContainer[eventType].Remove(deleteEventlist[i]);
                }

                return(true);
            }

            return(false);
        }
示例#2
0
        private Type GetEventType(Type type)
        {
            Type interfaceType         = null;
            var  eventExportAttributes = type.GetCustomAttributes(typeof(EventExportAttribute), false);

            if (eventExportAttributes == null || eventExportAttributes.Length == 0)
            {
                return(interfaceType);
            }

            EventExportAttribute eventExportAttribute = eventExportAttributes[0] as EventExportAttribute;

            if (eventExportAttribute == null)
            {
                return(interfaceType);
            }

            interfaceType = eventExportAttribute.InterfaceType;

            return(interfaceType);
        }
示例#3
0
        internal static void RegisterType(Type type, string lifeCycle)
        {
            var exportAttributes = type.GetCustomAttributes(typeof(EventExportAttribute), false);

            if (exportAttributes == null || exportAttributes.Length == 0)
            {
                return;
            }

            EventExportAttribute exportAttribute = exportAttributes[0] as EventExportAttribute;

            if (exportAttribute.EventType == null)
            {
                return;
            }

            var eventType = exportAttribute.EventType;

            switch (GetObjectLifeCycle(lifeCycle))
            {
            case ObjectLifeCycle.Singleton:
                ObjectContainer.RegisterInstance(exportAttribute.InterfaceType, Activator.CreateInstance(type));

                if (eventHandlerContainer.ContainsKey(eventType))
                {
                    eventHandlerContainer[eventType].Add(new EventHandlerKeyValuePair()
                    {
                        InterfaceType = exportAttribute.InterfaceType,
                        ImplementType = type
                    });
                }
                else
                {
                    IList <EventHandlerKeyValuePair> tempTypes = new List <EventHandlerKeyValuePair>();
                    tempTypes.Add(new EventHandlerKeyValuePair()
                    {
                        InterfaceType = exportAttribute.InterfaceType,
                        ImplementType = type
                    });
                    eventHandlerContainer.Add(eventType, tempTypes);
                }
                break;

            case ObjectLifeCycle.New:
                if (eventHandlerContainer.ContainsKey(eventType))
                {
                    if (eventHandlerContainer[eventType].Where(pre => pre.ImplementType == type).Count() > 0)
                    {
                        return;
                    }

                    eventHandlerContainer[eventType].Add(new EventHandlerKeyValuePair()
                    {
                        InterfaceType = exportAttribute.InterfaceType,
                        ImplementType = type
                    });
                }
                else
                {
                    IList <EventHandlerKeyValuePair> tempTypes = new List <EventHandlerKeyValuePair>();
                    tempTypes.Add(new EventHandlerKeyValuePair()
                    {
                        InterfaceType = exportAttribute.InterfaceType,
                        ImplementType = type
                    });
                    eventHandlerContainer.Add(eventType, tempTypes);
                }

                ObjectContainer.RegisterType(exportAttribute.InterfaceType, type);

                ObjectContainer.RegisterType(type, type);

                break;
            }
        }