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

查看:18
本文介绍了.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.

推荐答案

来自 DidItWith.NET 博客的 Dustin Campbell 检查了几个失败的创建弱事件处理程序的尝试,然后展示了一个有效的、有效的、轻量级的实现:用弱事件处理程序解决问题.

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.

不过,理想情况下,Microsoft 会将这一概念引入语言本身.类似的东西:

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