日期:2014-05-17  浏览次数:20965 次

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
------解决方案--------------------
在窗体上添加