c# 移除類中所有事件的綁定


https://www.cnblogs.com/zszh/p/11321005.html

單例中為防止多處注冊事件引起異步觸發時發生報錯,網上找了一圈沒找到想要的方法。

【異常類型】:ArgumentException
【異常信息】:該委托必須有一個目標(且僅有一個目標)。

結合網上資料整合了個方法

        /// <summary>
        /// 移除所有注冊事件
        /// </summary>
        public void RemoveAllEvent()
        {
            var newType = this.GetType();
            foreach (var item in newType.GetEvents())
            {
                FieldInfo _Field = newType.GetField(item.Name, BindingFlags.Instance | BindingFlags.NonPublic);
                if (_Field != null)
                {
                    object _FieldValue = _Field.GetValue(this);
                    if (_FieldValue != null && _FieldValue is Delegate)
                    {
                        Delegate _ObjectDelegate = (Delegate)_FieldValue;
                        Delegate[] invokeList = _ObjectDelegate.GetInvocationList();
                        if (invokeList != null)
                        {
                            foreach (Delegate del in invokeList)
                            {
                                item.RemoveEventHandler(this, del);
                            }
                        }
                    }
                }
            }
        }

靜態類方法

        /// <summary>
        /// 移除此靜態類指定事件名稱的所有事件綁定
        /// </summary>
        /// <param name="eventname"></param>
        public static void RemoveAllEvent(string eventname)
        {
            System.Reflection.FieldInfo _Field = typeof(GlobalVariables).GetField(eventname, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
            if (_Field != null)
            {
                object _FieldValue = _Field.GetValue(typeof(GlobalVariables));
                if (_FieldValue != null && _FieldValue is Delegate)
                {
                    Delegate _ObjectDelegate = (Delegate)_FieldValue;
                    Delegate[] invokeList = _ObjectDelegate.GetInvocationList();
                    if (invokeList != null)
                    {
                        foreach (var item in typeof(GlobalVariables).GetEvents())
                        {
                            if (item.Name == eventname)
                            {
                                foreach (Delegate del in invokeList)
                                {
                                    item.RemoveEventHandler(typeof(GlobalVariables), del);
                                }
                                break;
                            }
                        }
                    }
                }
            }

        }

 

 

 

測試:

using System;
using System.Reflection;

namespace FormTest
{
    class Class1
    {
        public event Action OnTest;
        public event Action OnShow;

        /// <summary>
        /// 移除所有注冊事件
        /// </summary>
        public void RemoveAllEvent()
        {
            var newType = this.GetType();
            foreach (var item in newType.GetEvents())
            {
                FieldInfo _Field = newType.GetField(item.Name, BindingFlags.Instance | BindingFlags.NonPublic);
                if (_Field != null)
                {
                    object _FieldValue = _Field.GetValue(this);
                    if (_FieldValue != null && _FieldValue is Delegate)
                    {
                        Delegate _ObjectDelegate = (Delegate)_FieldValue;
                        Delegate[] invokeList = _ObjectDelegate.GetInvocationList();
                        if (invokeList != null)
                        {
                            foreach (Delegate del in invokeList)
                            {
                                item.RemoveEventHandler(this, del);
                            }
                        }
                    }
                }
            }
        }
    }
}
View Code
        private void button7_Click(object sender, EventArgs e)
        {
            Class1 cla = new Class1();
            cla.OnShow += cla_OnShow;
            cla.OnShow += cla_OnShow;
            cla.OnTest += cla_OnTest;

            cla.RemoveAllEvent();
        }

        void cla_OnTest()
        {
            throw new NotImplementedException();
        }

        void cla_OnShow()
        {
            throw new NotImplementedException();
        }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM