繁体   English   中英

自定义参数未删除的事件处理程序

[英]Event Handler With Custom Parameters Not Removing

我将自定义事件添加到DataGridView以处理单元格单击。 这需要一个传递给它的参数。

我首先尝试删除以前的事件处理程序,因为我每次都发送相同的DataGridView并填充它。

//To delete the old handle
        DataGridViewToPopulate.CellClick -= (sender, e) => DataGridView_CellClick(sender, e, SourceObject);    
//To add new handle
        DataGridViewToPopulate.CellClick += (sender, e) => DataGridView_CellClick(sender, e, SourceObject);

我的问题是,当它第二次运行并且SourceObject已更改时,该事件仍然具有原始的SourceObject发送给它(我相信永远不会删除原始的句柄)。

我需要动态删除所有CellClick事件(甚至全部,没有那么多)。

第二次调用-=运算符时,也许“ this”与第一次执行+ =时是不同的对象。 这样,-=不会分离第一个对象,并且您将在不删除第一个对象的情况下附加第二个对象。

如果是这样,您应该尝试找到分离第一个对象的方法。

我能够通过反思找到解决方案,

        private static void UnsubscribeOne(object target)
    {

        Delegate[] subscribers;
        Type targetType;
        string EventName = "EVENT_DATAGRIDVIEWCELLCLICK";

        targetType = target.GetType();

        do
        {
            FieldInfo[] fields = targetType.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);

            foreach (FieldInfo field in fields)
            {

                if (field.Name == EventName)
                {

                    EventHandlerList eventHandlers = ((EventHandlerList)(target.GetType().GetProperty("Events", (BindingFlags.FlattenHierarchy | (BindingFlags.NonPublic | BindingFlags.Instance))).GetValue(target, null)));
                    Delegate d = eventHandlers[field.GetValue(target)];

                    if ((!(d == null)))
                    {

                        subscribers = d.GetInvocationList();

                        foreach (Delegate d1 in subscribers)
                        {

                            targetType.GetEvent("CellClick").RemoveEventHandler(target, d1);

                        }

                        return;
                    }
                }
            }

            targetType = targetType.BaseType;

        } while (targetType != null);

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM