C# 委托事件的另类取消方法

Type t  =  button1.GetType();
            PropertyInfo pi 
=  t.GetProperty( " Events " , BindingFlags.Instance  |  BindingFlags.NonPublic);
            EventHandlerList ehl 
=  (EventHandlerList)pi.GetValue(button1,  null );
            FieldInfo fieldInfo 
=  ( typeof (Control)).GetField( " EventClick " , BindingFlags.Static  |  BindingFlags.NonPublic);
            Delegate d 
=  ehl[fieldInfo.GetValue( null )];
            
if  (d  !=   null )
            {
                
foreach  (Delegate temp  in  d.GetInvocationList())
                {
                    ehl.RemoveHandler(fieldInfo.GetValue(
null ), temp);
                }
            }
这个方法适合在不知道委托事件的详细情况下,取消所有的委托事件。。

你可能感兴趣的:(C#)