易丰科技

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 3364|回复: 0
打印 上一主题 下一主题

屏蔽控件所有已注册的事件

[复制链接]

111

主题

117

帖子

3588

积分

论坛元老

Rank: 8Rank: 8

积分
3588
跳转到指定楼层
楼主
发表于 2011-10-2 12:06:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. public class EventSuppressor
  2.     {
  3.         Control _source;
  4.         EventHandlerList _sourceEventHandlerList;
  5.         FieldInfo _headFI;
  6.         Dictionary<object, Delegate[]> _handlers;
  7.         PropertyInfo _sourceEventsInfo;
  8.         Type _eventHandlerListType;
  9.         Type _sourceType;


  10.         public EventSuppressor(Control control)
  11.         {
  12.             if (control == null)
  13.                 throw new ArgumentNullException("control", "An instance of a control must be provided.");

  14.             _source = control;
  15.             _sourceType = _source.GetType();
  16.             _sourceEventsInfo = _sourceType.GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
  17.             _sourceEventHandlerList = (EventHandlerList)_sourceEventsInfo.GetValue(_source, null);
  18.             _eventHandlerListType = _sourceEventHandlerList.GetType();
  19.             _headFI = _eventHandlerListType.GetField("head", BindingFlags.Instance | BindingFlags.NonPublic);
  20.         }

  21.         private void BuildList()
  22.         {
  23.             _handlers = new Dictionary<object, Delegate[]>();
  24.             object head = _headFI.GetValue(_sourceEventHandlerList);
  25.             if (head != null)
  26.             {
  27.                 Type listEntryType = head.GetType();
  28.                 FieldInfo delegateFI = listEntryType.GetField("handler", BindingFlags.Instance | BindingFlags.NonPublic);
  29.                 FieldInfo keyFI = listEntryType.GetField("key", BindingFlags.Instance | BindingFlags.NonPublic);
  30.                 FieldInfo nextFI = listEntryType.GetField("next", BindingFlags.Instance | BindingFlags.NonPublic);
  31.                 BuildListWalk(head, delegateFI, keyFI, nextFI);
  32.             }
  33.         }

  34.         private void BuildListWalk(object entry, FieldInfo delegateFI, FieldInfo keyFI, FieldInfo nextFI)
  35.         {
  36.             if (entry != null)
  37.             {
  38.                 Delegate dele = (Delegate)delegateFI.GetValue(entry);
  39.                 object key = keyFI.GetValue(entry);
  40.                 object next = nextFI.GetValue(entry);

  41.                 Delegate[] listeners = dele.GetInvocationList();
  42.                 if (listeners != null && listeners.Length > 0)
  43.                     _handlers.Add(key, listeners);

  44.                 if (next != null)
  45.                 {
  46.                     BuildListWalk(next, delegateFI, keyFI, nextFI);
  47.                 }
  48.             }
  49.         }

  50.         public void Resume()
  51.         {
  52.             if (_handlers == null)
  53.                 throw new ApplicationException("Events have not been suppressed.");

  54.             foreach (KeyValuePair<object, Delegate[]> pair in _handlers)
  55.             {
  56.                 for (int x = 0; x < pair.Value.Length; x++)
  57.                     _sourceEventHandlerList.AddHandler(pair.Key, pair.Value[x]);
  58.             }

  59.             _handlers = null;
  60.         }

  61.         public void Suppress()
  62.         {
  63.             if (_handlers != null)
  64.                 throw new ApplicationException("Events are already being suppressed.");

  65.             BuildList();

  66.             foreach (KeyValuePair<object, Delegate[]> pair in _handlers)
  67.             {
  68.                 for (int x = pair.Value.Length - 1; x >= 0; x--)
  69.                     _sourceEventHandlerList.RemoveHandler(pair.Key, pair.Value[x]);
  70.             }
  71.         }

  72.     }
复制代码
使用范例:
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             RemoveSizeChangeEvent(this.panel1);
  4.         }
  5.         private void RemoveSizeChangeEvent(Panel b)
  6.         {
  7.             EventSuppressor s = new EventSuppressor(this.panel1);
  8.             s.Suppress();
  9.         }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|易丰科技

GMT+8, 2024-10-13 01:25 , Processed in 0.042702 second(s), 21 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表