有一个事件处理程序已被添加? [英] Has an event handler already been added?

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

问题描述

有没有办法告诉如果事件处理程序已被添加到一个对象?我序列化对象的列表进入/退出会话状态,这样我们就可以使用基于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.

在温和的烦恼的契合,我刚添加的事件处理程序来访问该对象的获取属性。它获取调用现在这是伟大的,但它是越来越被称为像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.

这可能吗?

编辑:我不一定有完全控制哪些事件处理程序添加的,所以只检查空不够好。

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

推荐答案

在定义类之外,作为@Telos提到,你只能使用事件处理程序上的左手侧 + = = - 。所以,如果你需要修改定义类的功能,可以提供通过检查事件处理程序来执行检查的方法 - 如果是这样,那么没有事件处理程序已被添加。如果没有,那么也许你可以通过在值循环 <一href="http://msdn.microsoft.com/en-us/library/system.delegate.getinvocationlist.aspx">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;
}

这可以很容易地修改为添加处理程序,如果它不存在。如果您没有访问类的内脏多数民众赞成暴露的情况下,你可能需要探索 = - + = 建议的那样,@ 娄佛朗哥

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天全站免登陆