关于处理对象删除处理程序 [英] Remove handlers on disposing object

查看:78
本文介绍了关于处理对象删除处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能想到的一些凌乱的方式来解决这个问题,但它让我觉得应该有一个更优雅的解决方案比那些我已经想出。

什么是一个对象之前,清理所有的事件处理程序的本身被设置最合适的方式。这是一个耻辱的事件处理程序将不可枚举。

在理论上,是它认为更正确的code添加处理程序的对象要记得删除它不是假设的对象将自身清洁之前它超出范围?

解决方案
  

在理论上,它被认为是更   正确的为code添加   处理程序的目的是要记得   删除它不是假设的对象   之前它去将自身清洁起来   超出范围?

要对上述问题,我会说是的。有关事件的基本原理是,事件射手不应该负责管理自己的处理程序;谁添加的事件应该做清理工作。

I can think of a few messy ways to solve this, but it strikes me that there should be a far more elegant solution than those which I've already come up with.

What's the most appropriate way for an object to cleanse itself of all of its event handlers prior to being disposed. It's a shame the event handler can't be enumerated upon.

In theory, is it considered more correct for the code adding the handler to an object to remember to remove it than assuming the object will clean itself up before it goes out of scope?

解决方案

In theory, is it considered more correct for the code adding the handler to an object to remember to remove it than assuming the object will clean itself up before it goes out of scope?

To the above question, I'll have to say yes. The basic theory about events is that the event firer shouldn't be responsible for managing its own handlers; whoever added the event should do cleanup.

这篇关于关于处理对象删除处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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