Пример #1
0
            public Subscription(SubscriptionDic <T, TSubscriber> sdic, Type targetType, object target, string eventName)
            {
                _sdic      = sdic;
                _target    = target;
                _eventName = eventName;
                Dictionary <string, EventInfo> evDic;

                if (!Accessors.TryGetValue(targetType, out evDic))
                {
                    Accessors[targetType] = evDic = new Dictionary <string, EventInfo>();
                }

                if (!evDic.TryGetValue(eventName, out _info))
                {
                    var ev = targetType.GetRuntimeEvents().FirstOrDefault(x => x.Name == eventName);

                    if (ev == null)
                    {
                        throw new ArgumentException(
                                  $"The event {eventName} was not found on {target.GetType()}.");
                    }

                    evDic[eventName] = _info = ev;
                }

                var del = new Action <object, T>(OnEvent);

                _delegate = del.GetMethodInfo().CreateDelegate(_info.EventHandlerType, del.Target);
                _info.AddMethod.Invoke(target, new[] { _delegate });
            }
Пример #2
0
            public Subscription(SubscriptionDic <T> sdic, object target, string eventName)
            {
                _sdic      = sdic;
                _target    = target;
                _eventName = eventName;
                var t = target.GetType();
                Dictionary <string, EventInfo> evDic;

                if (!Accessors.TryGetValue(t, out evDic))
                {
                    Accessors[t] = evDic = new Dictionary <string, EventInfo>();
                }
                if (!evDic.TryGetValue(eventName, out _info))
                {
                    evDic[eventName] = _info = t.GetRuntimeEvent(eventName);
                }

                var del = new Action <object, T>(OnEvent);

                _delegate = del.GetMethodInfo().CreateDelegate(_info.EventHandlerType, del.Target);
                _info.AddEventHandler(target, _delegate);
            }