首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#订阅事件是用了Lambda,如何取消订阅呢

2013-12-10 
c#订阅事件是用了Lambda,怎么取消订阅呢?代码如下:panel.DoubleClick + new EventHandler((sender, e)

c#订阅事件是用了Lambda,怎么取消订阅呢?
代码如下:
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)


        {
            PropertyInfo _PropertyInfo = p_Object.GetType().GetProperty("Events", BindingFlags.Instance 
[解决办法]
 BindingFlags.NonPublic);
            if (_PropertyInfo != null)
            {
                object _EventList = _PropertyInfo.GetValue(p_Object, null);
                if (_EventList != null && _EventList is EventHandlerList)
                {
                    EventHandlerList _List = (EventHandlerList)_EventList;
                    FieldInfo _FieldInfo = p_EventType.GetField(p_EventName, BindingFlags.Static 
[解决办法]
 BindingFlags.NonPublic 
[解决办法]
 BindingFlags.IgnoreCase);
                    if (_FieldInfo == null) return null;
                    Delegate _ObjectDelegate = _List[_FieldInfo.GetValue(p_Object)];
                    if (_ObjectDelegate == null) return null;
                    return _ObjectDelegate.GetInvocationList();
                }
            }
            return null;
        }
    }
}


[解决办法]

 EventHandler handler = (object sender1, EventArgs e1) =>
        {
            MessageBox.Show("regist");
        };

        private void button1_Click(object sender, EventArgs e)
        {
            this.button3.Click += handler;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.button3.Click -= handler;
        }

热点排行