在.NET弱的事件?事件、NET

2023-09-02 21:10:23 作者:森系女孩丶

如果对象A侦听到的对象B的事件,对象B将继续反对为生。 有没有一个标准的实施,将prevent这种弱的事件? 我知道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 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.