委托的清空

来源:互联网 发布:反美颜的软件 编辑:程序博客网 时间:2024/04/30 08:05
 (1).在类中申明清空委托方法,依次循环去除委托引用。
 
public MethodDelegate OnDelegate;
public void ClearDelegate() { while (this.OnDelegate != null) { this.OnDelegate -= this.OnDelegate; } } 
 
(2).如果在类中没有申明清空委托的方法,我们可以利用GetInvocationList查询出委托引用,然后进行去除。
 
public MethodDelegate OnDelegate;     
static
 void Main(string[] args) 

     Program test 
new Program(); 
     
if (test.OnDelegate != null
     { 
         System.Delegate[] dels 
= test.OnDelegate.GetInvocationList(); 
         
for (int i = 0; i < dels.Length; i++)
         { test.OnDelegate 
-= dels[i] as MethodDelegate; } 
     }
}
0 0