Example #1
0
        public static IEnumerable <MethodInfo> GetSubscribedMethods(this EventInfo ev, object instance)
        {
            var flags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.GetField;

            if (instance == null)
            {
                flags |= BindingFlags.Static;
            }

            FieldInfo f = ev.DeclaringType.GetField(ev.Name, flags);

            if (f != null)
            {
                var d = (System.Delegate)f.GetValue(instance);
                return(d.GetInvocationList().Select(s => s.Method));
            }
            else
            {
                // it's a custom implementation of event handler, handle the most used cases
                if (instance is System.ComponentModel.Component)
                {
                    var c = (System.ComponentModel.Component)instance;

                    var evList = (System.ComponentModel.EventHandlerList)c.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic)
                                 .GetValue(instance, null);

                    // magic scanner here that will probably not work all the time
                    // in System.Windows.Forms the Events are stored in an EventHandlerList
                    // the keys of those events are static fields defined, but no standard naming is used
                    // in Control it's all Event<EventName>, in Form it's EVENT_<EVENTNAME>

                    // best way to determine actual field is to inspect the IL of the add method, find the static private field info that
                    // is used

                    FieldInfo eventFieldKey = null;

                    MethodInfo addMethod = ev.GetAddMethod();
                    while (addMethod != null)
                    {
                        var instructions = addMethod.GetILInstructions();

                        eventFieldKey = instructions.Where(i => i.Code == OpCodes.Ldsfld && ((FieldInfo)i.Operand).Name.ToLower().StartsWith("event"))
                                        .Select(i => (FieldInfo)i.Operand)
                                        .FirstOrDefault();

                        if (eventFieldKey != null)
                        {
                            Delegate d = evList[eventFieldKey.GetValue(instance)];
                            if (d != null)
                            {
                                return(d.GetInvocationList().Select(s => s.Method));
                            }
                            else
                            {
                                return(Enumerable.Empty <MethodInfo>());
                            }
                        }
                        else
                        {
                            // it's possible that it's defined in base.addMethod
                            // go look for the base call
                            var baseMethod = instructions.Where(i => i.Code == OpCodes.Call && ((MethodInfo)i.Operand).Name == addMethod.Name)
                                             .Select(i => (MethodInfo)i.Operand)
                                             .FirstOrDefault();

                            addMethod = baseMethod;
                        }
                    }
                }

                else if (instance is System.Windows.UIElement)
                {
                    var c = (System.Windows.UIElement)instance;

                    var evStore = c.GetType().GetProperty("EventHandlersStore", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy)
                                  .GetValue(c, null);

                    if (evStore != null)
                    {
                        MethodInfo getRoutedEventHandlers = evStore.GetType().GetMethod("GetRoutedEventHandlers", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

                        MethodInfo addMethod = ev.GetAddMethod();
                        while (addMethod != null)
                        {
                            var       instructions  = addMethod.GetILInstructions();
                            FieldInfo eventFieldKey = null;

                            // ClickEvent, SubmenuClosedEvent, etc..
                            eventFieldKey = instructions.Where(i => i.Code == OpCodes.Ldsfld && ((FieldInfo)i.Operand).Name.ToLower().EndsWith("event"))
                                            .Select(i => (FieldInfo)i.Operand)
                                            .FirstOrDefault();

                            if (eventFieldKey != null)
                            {
                                System.Windows.RoutedEvent re = (System.Windows.RoutedEvent)eventFieldKey.GetValue(null);

                                var eventhandlers = (System.Windows.RoutedEventHandlerInfo[])getRoutedEventHandlers.Invoke(evStore, new object[] { re });

                                List <MethodInfo> methods = new List <MethodInfo>();
                                foreach (var evhandler in eventhandlers)
                                {
                                    Delegate d = evhandler.Handler;
                                    if (d != null)
                                    {
                                        methods.Add(d.Method);
                                    }
                                }
                                return(methods);
                            }
                            else
                            {
                                // it's possible that it's defined in base.addMethod
                                // go look for the base call
                                var baseMethod = instructions.Where(i => i.Code == OpCodes.Call && ((MethodInfo)i.Operand).Name == addMethod.Name)
                                                 .Select(i => (MethodInfo)i.Operand)
                                                 .FirstOrDefault();
                                addMethod = baseMethod;
                            }
                        }
                    }
                }
            }

            throw new Exception("Unable to evaluate event, custom event wiring is used");
        }
 public TextChangedEventArgs(System.Windows.RoutedEvent id, UndoAction action)
 {
 }
 public TextChangedEventArgs(System.Windows.RoutedEvent id, UndoAction action, ICollection <TextChange> changes)
 {
 }
 public DialogRoutedEventArgs(DialogResult dialogResult, System.Windows.RoutedEvent routedEvent, object source)
     : base(routedEvent, source)
 {
     this.dialogResult = dialogResult;
 }
 public DialogRoutedEventArgs(DialogResult dialogResult, System.Windows.RoutedEvent routedEvent)
     : base(routedEvent)
 {
     this.dialogResult = dialogResult;
 }
 public SelectionChangedEventArgs(System.Windows.RoutedEvent id, System.Collections.IList removedItems, System.Collections.IList addedItems)
 {
 }