.NET中的弱事件? [英] Weak events in .NET?

查看:104
本文介绍了.NET中的弱事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果对象A侦听来自对象B的事件,对象B将保持对象A存活。
是否有标准执行弱事件会阻止这种情况?
我知道WPF有一些机制,但我正在寻找一些不受WPF绑定的东西。
我猜测解决方案应该在某处使用弱引用。

If object A listens to an event from object B, object B will keep object A alive. Is there a standard implementation of weak events that would prevent this? I know WPF has some mechanism but I am looking for something not tied to WPF. I am guessing the solution should use weak references somewhere.

推荐答案

Dustin Campbell从DidItWith.NET博客中检查了几个尝试创建弱事件处理程序的失败尝试,然后继续显示一个有效,工作,轻量级的实现:使用弱事件处理程序解决问题

Dustin Campbell from the DidItWith.NET blog examines several of the failed attempts to create weak event handlers, then goes on to show a valid, working, lightweight implementation: Solving the Problem With Weak Event Handlers.

但理想情况下,微软会把这个概念引入语言本身。如下所示:

Ideally, though, Microsoft would introduce the concept into the language itself. Something like:

Foo.Clicked += new weak EventHandler(...);

如果您觉得此功能对您很重要,请在这里投票

If you feel this feature is important to you, please vote for it here.

这篇关于.NET中的弱事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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