Ejemplo n.º 1
0
 private static object GetHead(EventHandlerList eh)
 {
     try
     {
         FieldInfo fHead = eh.GetType().GetField("head", AllBindings);
         if (fHead == null)
         {
             fHead = eh.GetType().GetField("_head", AllBindings);
         }
         return(fHead.GetValue(eh));
     }
     catch { }
     return(null);
 }
        //[HostProtection(SecurityAction.LinkDemand, SharedState = true)]
        public object GetHead()
        {
            var field = _eventHandlerList.GetType()
                        .GetField("head", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField);

            if (field != null)
            {
                return(field.GetValue(_eventHandlerList));
            }
            return(null);
        }
Ejemplo n.º 3
0
 public cEventSuppressor(Control control)
 {
     if (control == null)
     {
         throw new ArgumentNullException("control", "An instance of a control must be provided.");
     }
     _source                 = control;
     _sourceType             = _source.GetType();
     _sourceEventsInfo       = _sourceType.GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
     _sourceEventHandlerList = (EventHandlerList)_sourceEventsInfo.GetValue(_source, null);
     _eventHandlerListType   = _sourceEventHandlerList.GetType();
     _headFI                 = _eventHandlerListType.GetField("head", BindingFlags.Instance | BindingFlags.NonPublic);
 }
        public void DebugEventHandlers()
        {
            EventHandlerList events = (EventHandlerList)typeof(Component)
                                      .GetField("events", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField)
                                      .GetValue(this);

            object current = events.GetType()
                             .GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField)[0]
                             .GetValue(events);

            List <Delegate> delegates = new List <Delegate>();

            while (current != null)
            {
                delegates.Add((Delegate)GetField(current, "handler"));
                current = GetField(current, "next");
            }
        }
        /// <summary>
        /// Remove disposed components from list
        /// </summary>
        public static void CleanLists()
        {
            List <TextBox> textBoxToRemove = new List <TextBox>();
            List <Control> controlToRemove = new List <Control>();

            //clean attached list
            textBoxToRemove = new List <TextBox>();
            foreach (KeyValuePair <TextBox, EnhancedTextBoxHelper.Items[]> entry in _attached_TextBox)
            {
                if (entry.Key.IsDisposed)
                {
                    textBoxToRemove.Add(entry.Key);
                }
            }
            foreach (TextBox entry in textBoxToRemove)
            {
                EventHandlerList events    = typeof(TextBox).GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(entry, null) as EventHandlerList;
                object           current   = events.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField)[0].GetValue(events);
                List <Delegate>  delegates = new List <Delegate>();
                while (current != null)
                {
                    delegates.Add((Delegate)(current.GetType().GetField("handler", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField).GetValue(current)));
                    current = current.GetType().GetField("next", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField).GetValue(current);
                }
                EventInfo[] eventinfol = typeof(TextBox).GetEvents();
                foreach (Type eventinfotype in eventinfol.Select(r => r.EventHandlerType).Distinct())
                {
                    foreach (Delegate d in delegates.Where(r => r != null && r.GetType() == eventinfotype))
                    {
                        foreach (EventInfo eventinfo in eventinfol.Where(r => r.EventHandlerType == eventinfotype))
                        {
                            eventinfo.RemoveEventHandler(entry, d);
                        }
                    }
                }
            }
            foreach (TextBox entry in textBoxToRemove)
            {
                _attached_TextBox.Remove(entry);
            }
            textBoxToRemove = new List <TextBox>();
            foreach (TextBox entry in _attached_KeyDown)
            {
                if (entry.IsDisposed)
                {
                    textBoxToRemove.Add(entry);
                }
            }
            foreach (TextBox entry in textBoxToRemove)
            {
                _attached_KeyDown.Remove(entry);
            }
            textBoxToRemove = new List <TextBox>();
            foreach (KeyValuePair <TextBox, MouseEventHandler> entry in _attached_MouseDown)
            {
                if (entry.Key.IsDisposed)
                {
                    textBoxToRemove.Add(entry.Key);
                }
            }
            foreach (TextBox entry in textBoxToRemove)
            {
                _attached_MouseDown.Remove(entry);
            }
            textBoxToRemove = new List <TextBox>();
            foreach (TextBox entry in _attached_MouseHover)
            {
                if (entry.IsDisposed)
                {
                    textBoxToRemove.Add(entry);
                }
            }
            foreach (TextBox entry in textBoxToRemove)
            {
                _attached_MouseHover.Remove(entry);
            }
            textBoxToRemove = new List <TextBox>();
            foreach (Control entry in _attached_parentMouseMove)
            {
                if (entry.IsDisposed)
                {
                    controlToRemove.Add(entry);
                }
            }
            foreach (Control entry in controlToRemove)
            {
                _attached_parentMouseMove.Remove(entry);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// winform获取控件某个事件委托
        /// </summary>
        /// <param name="component"></param>
        /// <param name="EventName"></param>
        /// <param name="EventHandlerTypeName"></param>
        /// <returns></returns>
        public Delegate[] GetComponentEventDelegate(Component component, string EventName, string EventHandlerTypeName)
        {
            Type             componentType      = component.GetType();
            PropertyInfo     eventsPropertyInfo = componentType.GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
            EventHandlerList eventHanlderList   = eventsPropertyInfo.GetValue(component, null) as EventHandlerList;
            FieldInfo        HeadFieldInfo      = eventHanlderList.GetType().GetField("head", BindingFlags.Instance | BindingFlags.NonPublic);
            object           HeadObject         = HeadFieldInfo.GetValue(eventHanlderList);

            do
            {
                FieldInfo[] fieldInfoList = componentType.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
                foreach (FieldInfo fieldInfo in fieldInfoList)
                {
                    object fieldValue = fieldInfo.GetValue(component);
                    if (fieldValue != null)
                    {
                        Type fieldType = fieldValue.GetType();
                        if (fieldType.Name == EventHandlerTypeName && (fieldValue as Delegate) != null)
                        {
                            return((fieldValue as Delegate).GetInvocationList());
                        }
                        else if (fieldType.Name == typeof(Object).Name)
                        {
                            if (fieldInfo.Name.IndexOf(EventName, StringComparison.OrdinalIgnoreCase) > -1)
                            {
                                if (HeadObject != null)
                                {
                                    Delegate delegateObject = eventHanlderList[fieldValue];
                                    if (delegateObject != null)
                                    {
                                        return(delegateObject.GetInvocationList());
                                    }
                                }
                            }
                        }
                    }
                }
                componentType = componentType.BaseType;
            } while (componentType != null);

            if (HeadObject != null)
            {
                object    ListEntry        = HeadObject;
                Type      ListEntryType    = ListEntry.GetType();
                FieldInfo handlerFieldInfo = ListEntryType.GetField("handler", BindingFlags.Instance | BindingFlags.NonPublic);
                FieldInfo keyFieldInfo     = ListEntryType.GetField("key", BindingFlags.Instance | BindingFlags.NonPublic);
                FieldInfo nextFieldInfo    = ListEntryType.GetField("next", BindingFlags.Instance | BindingFlags.NonPublic);

                while (ListEntry != null)
                {
                    Delegate handler = handlerFieldInfo.GetValue(ListEntry) as Delegate;
                    object   key     = keyFieldInfo.GetValue(ListEntry);
                    ListEntry = nextFieldInfo.GetValue(ListEntry);

                    if (handler != null && handler.GetType().Name == EventHandlerTypeName)
                    {
                        return(handler.GetInvocationList());
                    }
                }
            }
            return(null);
        }