Beispiel #1
0
        public void RegisterEvents(object instance)
        {
            var type             = instance.GetType();
            var subscribeSchemas = SubscribeSchema.Get(type.FullName);

            if (subscribeSchemas == null)
            {
                return;
            }

            foreach (var subscribeSchema in subscribeSchemas)
            {
                var subInstance = new object(); // ServiceFactory.CreateInstance(subscribeSchema.SubscribeEventEnclosingType);

                EventInfo eventInfo   = subscribeSchema.SubscribeEventEnclosingType.GetEvent(subscribeSchema.SubscribeEventName);
                Type      typeOfEvent = eventInfo.EventHandlerType;
                Delegate  handler     = Delegate.CreateDelegate(typeOfEvent, instance, subscribeSchema.SubscriberMethod);

                var eventDelegate = EventHelper.GetEventHandler(subInstance, subscribeSchema.SubscribeEventName);
                if (eventDelegate == null)
                {
                    eventInfo.AddEventHandler(subInstance, handler);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine(type.FullName + "Listeners: " + eventDelegate.GetInvocationList().Length);
                }
            }
        }
        public static SubscribeSchema Add(string typeFullName, SubscribeSchema model)
        {
            List <SubscribeSchema> list;

            if (SubscribeDictionary.TryGetValue(typeFullName, out list))
            {
                var existedModel = list.FirstOrDefault(q => q.SubscribeEventName == model.SubscribeEventName && q.SubscriberMethod == model.SubscriberMethod);
                if (existedModel == null)
                {
                    list.Add(model);
                }
                else
                {
                    existedModel = model;
                }
                return(model);
            }
            else
            {
                SubscribeDictionary.Add(typeFullName, new List <SubscribeSchema>()
                {
                    model
                });
                return(model);
            }
            return(null);
        }
Beispiel #3
0
        public void Fire()
        {
            Assembly[] assemblies   = AppDomain.CurrentDomain.GetAssemblies().Where(q => q.FullName.Contains("Business")).ToArray();
            var        bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;

            var interfaceType = typeof(Interface.IService <>);
            var types         = assemblies
                                .SelectMany(s => s.GetTypes())
                                .Where(m => m.GetCustomAttributes(typeof(Service.ServiceAttribute), true).Any()).ToList();

            foreach (var type in types)
            {
                var methods = type.GetMethods(bindingFlags)
                              .Where(m => m.GetCustomAttributes(typeof(Eventing.SubscribeToAttribute), true).Any()).ToList();

                foreach (var method in methods)
                {
                    var attributes = method.GetCustomAttributes().ToList();
                    foreach (var attribute in attributes)
                    {
                        var attr                 = (Eventing.SubscribeToAttribute)attribute;
                        var eventName            = attr.EventName;
                        var serviceInterfaceType = attr.ServiceInterfaceType;
                        var methodName           = method.Name;

                        SubscribeSchema.Add(type.FullName, new SubscribeSchema()
                        {
                            SubscribeEventName          = eventName,
                            SubscribeEventEnclosingType = serviceInterfaceType,
                            SubscriberMethod            = method,
                            SubscriberType = type
                        });
                    }
                }
            }
        }