Example #1
0
        /// <summary>
        /// 向总线上注册事件
        /// </summary>
        ///<param name="handlerType">处理器类型</param>
        /// <returns>返回操作的结果</returns>
        public bool RegisterHandler(Type handlerType)
        {
            if (handlerType == null || handlerType.IsInterface)
            {
                return(false);
            }
            var exportAttributes = handlerType.GetCustomAttributes(typeof(RuleExportAttribute), false);

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

            RuleExportAttribute exportAttribute = exportAttributes[0] as RuleExportAttribute;

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

            Type eventType = null;

            if (exportAttribute.InterfaceType.GetGenericArguments().Length > 0)
            {
                eventType = exportAttribute.InterfaceType.GetGenericArguments()[0];
            }
            else
            {
                eventType = exportAttribute.InterfaceType;
            }

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

            if (ruleHandlerContainer.ContainsKey(eventType))
            {
                ruleHandlerContainer[eventType].Add(new RuleHandlerKeyValuePair()
                {
                    InterfaceType = exportAttribute.InterfaceType,
                    ImplementType = handlerType
                });
            }
            else
            {
                IList <RuleHandlerKeyValuePair> tempTypes = new List <RuleHandlerKeyValuePair>();
                tempTypes.Add(new RuleHandlerKeyValuePair()
                {
                    InterfaceType = exportAttribute.InterfaceType,
                    ImplementType = handlerType
                });
                ruleHandlerContainer.Add(eventType, tempTypes);
            }

            return(true);
        }
Example #2
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(RuleExportAttribute), false);

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

            RuleExportAttribute exportAttribute = exportAttributes[0] as RuleExportAttribute;

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

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

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

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

                return(true);
            }

            return(false);
        }
Example #3
0
        private Type GetRuleType(Type type)
        {
            Type interfaceType        = null;
            var  ruleExportAttributes = type.GetCustomAttributes(typeof(RuleExportAttribute), false);

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

            RuleExportAttribute ruleExportAttribute = ruleExportAttributes[0] as RuleExportAttribute;

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

            interfaceType = ruleExportAttribute.InterfaceType;

            return(interfaceType);
        }
Example #4
0
        internal static void RegisterType(Type type, string lifeCycle)
        {
            var exportAttributes = type.GetCustomAttributes(typeof(RuleExportAttribute), false);

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

            RuleExportAttribute exportAttribute = exportAttributes[0] as RuleExportAttribute;

            switch (GetObjectLifeCycle(lifeCycle))
            {
            case ObjectLifeCycle.Singleton:
                unityContainer.RegisterInstance(exportAttribute.InterfaceType, Activator.CreateInstance(type));
                var tempArguments = exportAttribute.InterfaceType.GetGenericArguments();
                var eventType     = tempArguments.Count() > 0 ? tempArguments[0] : exportAttribute.InterfaceType;

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

            case ObjectLifeCycle.New:
                //foreach (var tempInstance in objectInterfaces)
                unityContainer.RegisterType(exportAttribute.InterfaceType, type);
                var tempArguments1 = exportAttribute.InterfaceType.GetGenericArguments();
                var eventType1     = tempArguments1.Count() > 0 ? tempArguments1[0] : exportAttribute.InterfaceType;

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