是否已经添加了事件处理程序? [英] Has an event handler already been added?

查看:20
本文介绍了是否已经添加了事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法判断事件处理程序是否已添加到对象中?我正在将对象列表序列化为/退出会话状态,以便我们可以使用基于 SQL 的会话状态.......但是,现在当对象被反序列化时,它不会获得事件处理程序.

Is there a way to tell if an event handler has been added to an object? I'm serializing a list of objects into/out of session state so we can use SQL based session state... When an object in the list has a property changed it needs to be flagged, which the event handler took care of properly before. However now when the objects are deserialized it isn't getting the event handler.

出于轻微的烦恼,我只是将事件处理程序添加到访问对象的 Get 属性中.现在它被调用了,这很好,除了它被调用了 5 次,所以我认为每次访问对象时都会不断添加处理程序.

In an fit of mild annoyance, I just added the event handler to the Get property that accesses the object. It's getting called now which is great, except that it's getting called like 5 times so I think the handler just keeps getting added every time the object is accessed.

忽略它确实足够安全,但我宁愿通过检查处理程序是否已经添加来使它更干净,所以我只添加一次.

It's really safe enough to just ignore, but I'd rather make it that much cleaner by checking to see if the handler has already been added so I only do so once.

这可能吗?

我不一定完全控制添加的事件处理程序,因此仅检查 null 是不够的.

I don't necessarily have full control of what event handlers are added, so just checking for null isn't good enough.

推荐答案

从定义类的外部,正如@Telos 提到的,你只能在 += 的左侧使用 EventHandler或 -=.因此,如果您有能力修改定义类,您可以提供一种方法来通过检查事件处理程序是否为 null 来执行检查 - 如果是,则没有添加任何事件处理程序.如果没有,那么也许你可以遍历中的值Delegate.GetInvocationList.如果一个等于您要添加为事件处理程序的委托,那么您就知道它在那里.

From outside the defining class, as @Telos mentions, you can only use EventHandler on the left-hand side of a += or a -=. So, if you have the ability to modify the defining class, you could provide a method to perform the check by checking if the event handler is null - if so, then no event handler has been added. If not, then maybe and you can loop through the values in Delegate.GetInvocationList. If one is equal to the delegate that you want to add as event handler, then you know it's there.

public bool IsEventHandlerRegistered(Delegate prospectiveHandler)
{   
    if ( this.EventHandler != null )
    {
        foreach ( Delegate existingHandler in this.EventHandler.GetInvocationList() )
        {
            if ( existingHandler == prospectiveHandler )
            {
                return true;
            }
        }
    }
    return false;
}

这可以很容易地修改为如果它不存在,则添加处理程序".如果您无法访问公开事件的类的内部结构,则可能需要按照@Lou Franco 的建议探索 -=+=.

And this could easily be modified to become "add the handler if it's not there". If you don't have access to the innards of the class that's exposing the event, you may need to explore -= and +=, as suggested by @Lou Franco.

但是,您最好重新检查调试和停用这些对象的方式,看看是否无法找到自己跟踪这些信息的方法.

However, you may be better off reexamining the way you're commissioning and decommissioning these objects, to see if you can't find a way to track this information yourself.

这篇关于是否已经添加了事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆