zoukankan      html  css  js  c++  java
  • 移除全部事件委托

     public class Test
        {
            public event EventHandler AA;
            public void Foo()
            {
                if (AA != null) AA(this, new EventArgs());
            }
        }
    
    static void Main(string[] args)
            {
                Test obj = new Test(); 
                obj.AA += delegate { Console.WriteLine("event raised."); }; 
                obj.Foo(); 
                RemoveEvent<Test>(obj, "AA"); 
                obj.Foo(); 
                Console.ReadKey();
            }
    
            static void RemoveEvent<T>(T c, string name)
            {
                Delegate[] invokeList = GetObjectEventList(c, "AA"); 
                foreach (Delegate del in invokeList) 
                { 
                    typeof(T).GetEvent("AA").RemoveEventHandler(c, del); 
                }
            }
     
            ///  <summary>     
            /// 获取对象事件 zgke@sina.com qq:116149     
            ///  </summary>     
            ///  <param name="p_Object">对象 </param>     
            ///  <param name="p_EventName">事件名 </param>     
            ///  <returns>委托列 </returns>     
            public static Delegate[] GetObjectEventList(object p_Object, string p_EventName)
            {
                FieldInfo _Field = p_Object.GetType().GetField(p_EventName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);
                if (_Field == null) 
                { 
                    return null;
                } 
                object _FieldValue = _Field.GetValue(p_Object); 
                if (_FieldValue != null && _FieldValue is Delegate) 
                { 
                    Delegate _ObjectDelegate = (Delegate)_FieldValue; 
                    return _ObjectDelegate.GetInvocationList();
                } 
                return null;
            } 
  • 相关阅读:
    最近要看的项目
    Lavarel Route::resource
    架构,性能
    Unity ToLua & LuaFramework_UGUI学习笔记(zz)
    Unity UI 布局
    Introduction to Unity UI
    Unity more efficient find
    unity UI如何开启(显示)或者关闭(隐藏)Panel界面最好?
    Unity Canvas vs Panel
    Unity实现新手引导圆形遮罩
  • 原文地址:https://www.cnblogs.com/gxivwshjj/p/3711385.html
Copyright © 2011-2022 走看看