public void AddSubscription(PriorityDelegate priorityDelegate, Type eventType)
 {
     if (!subscriptions.ContainsKey(eventType))
     {
         subscriptions.Add(eventType, new List <PriorityDelegate>());
     }
     subscriptions[eventType].Add(priorityDelegate);
 }
Esempio n. 2
0
        public void Register(object objectToRegister, IRawSubscriber subscriber)
        {
            RawSubscribe singleDelegate = subscriber.SubscribeRaw;
            int          priority       = subscriber.GetPriority();

            AddReceiver(objectToRegister);
            PriorityDelegate priorityDelegate = AddSubscription(subscriber.GetEventType(), singleDelegate, priority);

            _receivers[objectToRegister].Add(priorityDelegate);
        }
Esempio n. 3
0
        private PriorityDelegate AddSubscription(Type eventType, Delegate d, int priority = 0)
        {
            PriorityDelegate priorityDelegate = new PriorityDelegate(priority, d);

            if (!_subscriptions.ContainsKey(eventType))
            {
                _subscriptions.Add(eventType, new SortedList <PriorityDelegate>());
            }

            _subscriptions[eventType].Add(priorityDelegate);
            _stage?.AddSubscription(priorityDelegate, eventType);
            return(priorityDelegate);
        }
Esempio n. 4
0
        public void Register(object objectToRegister, bool silentMode)
        {
            bool alreadyRegistered = !AddReceiver(objectToRegister);

            if (alreadyRegistered)
            {
                if (!silentMode)
                {
                    throw new RegisterObjectTwiceException();
                }

                return;
            }

            MethodInfo[] methods = objectToRegister.GetType().GetMethods(BindingFlags.NonPublic |
                                                                         BindingFlags.Instance | BindingFlags.Public);
            for (int m = 0; m < methods.Length; m++)
            {
                object[] attributes = methods[m].GetCustomAttributes(true);
                for (int i = 0; i < attributes.Length; i++)
                {
                    if (attributes[i] is Subscribe subscribe)
                    {
                        MethodInfo method = methods[m];
                        if (method.GetParameters().Length != 1)
                        {
                            continue;
                        }

                        ParameterInfo firstArgument = method.GetParameters()[0];
                        List <Type>   args          = new List <Type>(
                            method.GetParameters().Select(p => p.ParameterType));
                        Type delegateType;
                        if (method.ReturnType == typeof(void))
                        {
                            delegateType = Expression.GetActionType(args.ToArray());
                        }
                        else
                        {
                            args.Add(method.ReturnType);
                            delegateType = Expression.GetFuncType(args.ToArray());
                        }

                        Delegate         d = Delegate.CreateDelegate(delegateType, objectToRegister, method.Name);
                        PriorityDelegate priorityDelegate =
                            AddSubscription(firstArgument.ParameterType, d, subscribe.priority);
                        _receivers[objectToRegister].Add(priorityDelegate);
                    }
                }
            }
        }