コード例 #1
0
        private void ListenToEvents()
        {
            var type   = typeof(T);
            var events = type.GetEvents(BindingFlags.Instance | BindingFlags.Public);

            foreach (var e in events)
            {
                var eht = e.EventHandlerType;
                if (eht == typeof(EventHandler))
                {
                    if (e.Name.EndsWith("Changed"))
                    {
                        var          pName = e.Name.Substring(0, e.Name.Length - "Changed".Length);
                        PropertyInfo p     = null;
                        try
                        {
                            p = type.GetProperty(pName, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance);
                        }
                        catch (Exception) { }
                        e.AddEventHandler(Target, (EventHandler)((sender, ea) =>
                        {
                            ValueUpdate vu = null;
                            if (p != null && p.CanRead)
                            {
                                var newValue = p.GetValue(Target);
                                var vuType = typeof(ValueUpdate <>).MakeGenericType(p.PropertyType);
                                vu = (ValueUpdate)Activator.CreateInstance(vuType, newValue);
                            }
                            EventHandler(e.Name, sender, ea, vu);
                        }));
                        //Debug.WriteLine($"EventMonitor<{typeof(T).FullName}>: listening to property changes on {e.Name}");
                    }
                    else
                    {
                        e.AddEventHandler(Target, (EventHandler)((sender, ea) => EventHandler(e.Name, sender, ea)));
                        //Debug.WriteLine($"EventMonitor<{typeof(T).FullName}>: listening to {e.Name}");
                    }
                }
                else if (eht.IsGenericType && eht.GetGenericTypeDefinition() == typeof(EventHandler <>))
                {
                    var eventArgsType = eht.GetGenericArguments()[0];
                    if (eventArgsType.IsGenericType && eventArgsType.GetGenericTypeDefinition() == typeof(PropertyUpdateEventArgs <>))
                    {
                        var handlerTarget = new GenericPropertyUpdateEventHandlerTarget(this, e);
                        e.AddEventHandler(Target, handlerTarget.GetHandlerDelegate());
                        //Debug.WriteLine($"EventMonitor<{typeof(T).FullName}>: listening to property updates on {e.Name}");
                    }
                    else
                    {
                        var handlerTarget = new GenericEventHandlerTarget(this, e);
                        e.AddEventHandler(Target, handlerTarget.GetHandlerDelegate());
                        //Debug.WriteLine($"EventMonitor<{typeof(T).FullName}>: listening to generic {e.Name}");
                    }
                }
                else
                {
                    //Debug.WriteLine($"EventMonitor<{typeof(T).FullName}>: NOT listening to {e.Name}");
                }
            }
        }
コード例 #2
0
 public EventRecord(string eventName, object sender, EventArgs e, ValueUpdate vu = null)
 {
     EventName   = eventName;
     Sender      = sender;
     EventArgs   = e;
     ValueUpdate = vu;
 }
コード例 #3
0
        private void EventHandler(string eventName, object sender, EventArgs e, ValueUpdate vu = null)
        {
            var record = new EventRecord(eventName, sender, e, vu);

            lock (recordLock)
            {
                RecordedEvents.Add(record);
            }
            if (LogEvents)
            {
                Debug.WriteLine($"EventMonitor<{typeof(T).FullName}>: caught event from {eventName}");
            }
            CaughtEvent?.Invoke(this, new EventRecordEventArgs(record));
        }