c#订阅事件是用了Lambda,如何取消订阅呢
c#订阅事件是用了Lambda,怎么取消订阅呢?
代码如下:
panel.DoubleClick += new EventHandler((sender, e) =>{
//我怎么在这里面取消对这个事件的订阅呢?
}
//求大神解救
------解决方案--------------------
Action<object, EventArgs> pro = null;
pro = (sender, e) =>
{
//.......
panel.DoubleClick -= pro;
};
panel.DoubleClick += pro
------解决方案--------------------
参考:
http://social.microsoft.com/Forums/zh-CN/9211ba35-001f-4319-a8e6-96e53995fbf9/c-?forum=visualcshartzhchs
http://www.csharpwin.com/csharpspace/10743r4702.shtml
------解决方案--------------------
在窗体上添加3个按钮,然后编写如下代码:
代码如下:
panel.DoubleClick += new EventHandler((sender, e) =>{
//我怎么在这里面取消对这个事件的订阅呢?
}
//求大神解救
------解决方案--------------------
Action<object, EventArgs> pro = null;
pro = (sender, e) =>
{
//.......
panel.DoubleClick -= pro;
};
panel.DoubleClick += pro
------解决方案--------------------
panel1.DoubleClick += new EventHandler(
(sender1, e1)=>
{
//......
Type t = panel1.GetType();
PropertyInfo pi = t.GetProperty("Events", BindingFlags.Instance
------解决方案--------------------
BindingFlags.NonPublic);
EventHandlerList ehl = (EventHandlerList)pi.GetValue(panel1, null);
FieldInfo fieldInfo = (typeof(Control)).GetField("EventDoubleClick", BindingFlags.Static
------解决方案--------------------
BindingFlags.NonPublic);
Delegate d = ehl[fieldInfo.GetValue(null)];
if (d != null)
{
foreach (Delegate temp in d.GetInvocationList())
{
StackTrace st = new StackTrace(true);
if (temp.Method == st.GetFrame(0).GetMethod())
ehl.RemoveHandler(fieldInfo.GetValue(null), temp);
}
}
}
);
参考:
http://social.microsoft.com/Forums/zh-CN/9211ba35-001f-4319-a8e6-96e53995fbf9/c-?forum=visualcshartzhchs
http://www.csharpwin.com/csharpspace/10743r4702.shtml
------解决方案--------------------
在窗体上添加3个按钮,然后编写如下代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button3.Click += (a, b) => MessageBox.Show("ok");
}
private void button2_Click(object sender, EventArgs e)
{
var handlers = GetObjectEventList(button3, "EventClick", typeof(Control));
foreach (var item in handlers.Select(x => x as EventHandler))
button3.Click -= item;
}
private Delegate[] GetObjectEventList(object p_Object, string p_EventName, Type p_EventType)