Пример #1
0
        public void RegisterSubscription(object instance)
        {
            Type type = instance.GetType();

            object[] attributes = type.GetCustomAttributes(typeof(SubscribtionAttribute), false);

            if (attributes.Length == 0)
            {
                throw new Exception("SubscriptionAttribute is missing");
            }

            foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance))
            {
                object[] methodAttributes = method.GetCustomAttributes(typeof(MessageSubscriptionAttribute), false);

                if (methodAttributes.Length == 0)
                {
                    continue;
                }

                ParameterInfo[] parameterInfos = method.GetParameters();

                if (parameterInfos.Length != 1)
                {
                    throw new Exception(
                              "Method annotated by MessageSubscriptionAttribute must have exactly one parameter");
                }

                ParameterInfo parameterInfo = parameterInfos[0];
                MessageSubscriptionAttribute messageSubscriptionAttribute = (MessageSubscriptionAttribute)methodAttributes[0];

                Type         parameterType = parameterInfo.ParameterType;
                ICallHandler handler;
                Type         dataType;

                MethodInfo      m      = method;
                Action <object> action = o => m.Invoke(instance, new[] { o });

                if (parameterType.IsGenericType && parameterType.GetGenericTypeDefinition() == typeof(BusMessage <>))
                {
                    Type[] genericArguments = parameterType.GetGenericArguments();

                    dataType = genericArguments[0];

                    Type handlerType = typeof(BusMessageCallHandler <>).MakeGenericType(genericArguments);

                    handler = (ICallHandler)Activator.CreateInstance(handlerType, action);
                }
                else
                {
                    dataType = parameterType;

                    handler = new ActionHandler <object>(action);
                }

                List <BusHeader> filters          = new List <BusHeader>();
                object[]         filterAttributes = parameterInfo.GetCustomAttributes(typeof(HeaderFilterAttribute), true);

                foreach (HeaderFilterAttribute filterAttribute in filterAttributes)
                {
                    filters.Add(new BusHeader(filterAttribute.Name, filterAttribute.Value));
                }

                Subscribe(dataType, handler, messageSubscriptionAttribute.RegisterHierarchy, filters);
            }
        }
Пример #2
0
        public bool Subscribe <TData>(Action <TData> callback, bool hierarchy, IEnumerable <BusHeader> filter)
        {
            ActionHandler <TData> actionHandler = new ActionHandler <TData>(callback);

            return(_helper.Subscribe(typeof(TData), actionHandler, hierarchy, filter));
        }
Пример #3
0
        public bool Subscribe(Type dataType, Action <object> callback, bool hierarchy, IEnumerable <BusHeader> filter)
        {
            ActionHandler <object> actionHandler = new ActionHandler <object>(callback);

            return(_helper.Subscribe(dataType, actionHandler, hierarchy, filter));
        }
Пример #4
0
        public void RegisterSubscription(object instance)
        {
            Type type = instance.GetType();

            object[] attributes = type.GetCustomAttributes(typeof(SubscribtionAttribute), false);

            if (attributes.Length == 0)
            {
                throw new Exception("SubscriptionAttribute is missing");
            }

            foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance))
            {
                object[] methodAttributes = method.GetCustomAttributes(typeof (MessageSubscriptionAttribute), false);

                if (methodAttributes.Length == 0)
                {
                    continue;
                }

                ParameterInfo[] parameterInfos = method.GetParameters();

                if (parameterInfos.Length != 1)
                {
                    throw new Exception(
                        "Method annotated by MessageSubscribtionAttribute must have exactly one parameter");
                }

                ParameterInfo parameterInfo = parameterInfos[0];
                MessageSubscriptionAttribute messageSubscriptionAttribute = (MessageSubscriptionAttribute) methodAttributes[0];

                Type parameterType = parameterInfo.ParameterType;
                ICallHandler handler;
                Type dataType;

                MethodInfo m = method;
                Action<object> action = o => m.Invoke(instance, new[] {o});

                if (parameterType.IsGenericType && parameterType.GetGenericTypeDefinition() == typeof (BusMessage<>))
                {
                    Type[] genericArguments = parameterType.GetGenericArguments();

                    dataType = genericArguments[0];

                    Type handlerType = typeof (BusMessageHandler<>).MakeGenericType(genericArguments);

                    handler = (ICallHandler) Activator.CreateInstance(handlerType, action);
                }
                else
                {
                    dataType = parameterType;

                    handler = new ActionHandler(action);
                }

                List<BusHeader> filters = new List<BusHeader>();
                object[] filterAttributes = parameterInfo.GetCustomAttributes(typeof (HeaderFilterAttribute), true);

                foreach (HeaderFilterAttribute filterAttribute in filterAttributes)
                {
                    filters.Add(new BusHeader(filterAttribute.Name, filterAttribute.Value));
                }

                Subscribe(dataType, handler, messageSubscriptionAttribute.RegisterHierarchy, filters);
            }
        }