清理(委托类型实例)事件处理(实例)的函数及Lambda表达式

 1 namespace Microshaoft
 2 {
 3     using System;
 4     using System.Linq;
 5     using System.Drawing;
 6     using System.Collections.Generic;
 7     using System.Windows.Forms;
 8     public class Form1 : Form
 9     {
10         static void Main()
11         {
12             Application.Run(new Form1());
13         }
14         //private List<Action<object, EventArgs>> _actions = null;
15         //private List<EventHandler> _eventHandlers = null;
16         private Button button1;
17         public Form1()
18         {
19             button1 = new Button();
20             button1.Text = "button1";
21             Controls.Add(button1);
22             
23             var actions = new List<Action<object, EventArgs>>();
24             //_actions = actions;
25             var eventHandlers = new List<EventHandler>();
26             //_eventHandlers = eventHandlers;
27             //int i = 1;
28             EventHandler eh;
29             eh = new EventHandler
30                             (
31                                 (x, y) =>
32                                 {
33                                     MessageBox.Show("Lambda1");
34                                     DisposeHandlersAllProcesses(eventHandlers);
35                                 }
36                             );
37             eventHandlers.Add(eh);
38             eventHandlers.Add(eh);
39             //_actions.Add(eh);
40             //_actions.Add(eh);
41             eventHandlers
42                 //_actions
43                 .ForEach
44                                 (
45                                     (x) =>
46                                     {
47                                         button1.Click += eh;
48                                         button1.Click +=
49                                                     (
50                                                         (xx, yy) =>
51                                                         {
52                                                             MessageBox.Show("Lambda2");
53                                                             DisposeHandlersAllProcesses(eventHandlers);
54                                                         }
55                                                     );
56                                     }
57                                 );
58             button1.Click -= eh;
59             eh = null;
60             DisposeHandlersAllProcesses(eventHandlers);
61             //(EventHandler) _action;
62             //new EventHandler
63         }
64         //private void DisposeHandlersAllProcesses(List<Action> handlers)
65         private void DisposeHandlersAllProcesses(List<EventHandler> handlers)
66         {
67             handlers
68                 //_actions
69                     .ForEach
70                         (
71                             (x) =>
72                             {
73                                 var invocations = x.GetInvocationList();
74                                 Array.ForEach
75                                         (
76                                             invocations
77                                             , (xx) =>
78                                             {
79                                                 x -= (EventHandler)xx;
80                                                 Console.WriteLine("{0}:{1}", x, xx);
81                                             }
82                                         );
83                                 x = null;
84                             }
85                         );
86         }
87     }
88 }

 

转载于:https://www.cnblogs.com/Microshaoft/archive/2012/12/29/2839310.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值