我正在附加到第 3 方,长期存在的 Deleted 事件发布者,由于事件处理程序,它最终使我可能短暂存在的对象保持活动状态。Deleted 事件很可能永远不会触发,如果是,我只需要处理。取消订阅 Deleted 事件的位置并不明显,因此我希望对它进行弱引用,以便我的对象可以被 GC 处理。
我已经看到了很多非常精细的方法来创建弱事件处理程序,但是下面的代码片段似乎可以解决问题,至少在提供的测试代码片段中是这样。这只是疯了还是可以工作?
(http://diditwith.net/CommentView,guid,aacdb8ae-7baa-4423-a953-c18c1c7940ab.aspx在“A First Stab”下说类似的片段“(...)不够健壮,无法与一个事件(...)”,为什么不呢?)
public static class WeakEvent
{
private class WeakEventHolder<TArgs> where TArgs : EventArgs
{
private readonly WeakReference _handler;
public WeakEventHolder(Action<object, TArgs> handler)
{
_handler = new WeakReference(handler);
}
public void Handle(object sender, TArgs args)
{
Action<object, TArgs> handler = (Action<object, TArgs>)_handler.Target;
if (handler != null)
handler(sender, args);
}
}
public static EventHandler MakeHandler(Action<object, EventArgs> handler)
{
return new WeakEventHolder<EventArgs>(handler).Handle;
}
}
测试班
[TestFixture]
public class Tests
{
public class Publisher
{
public EventHandler Event;
public void Raise()
{
if (Event != null)
Event(this, EventArgs.Empty);
}
}
public class Target
{
public Target(Publisher publisher)
{
publisher.Event += WeakEvent.MakeHandler(HandleEvent);
}
public void HandleEvent(object sender, EventArgs args)
{
System.Diagnostics.Trace.WriteLine("HandleEvent");
}
}
[Test]
public void Test()
{
Publisher publisher = new Publisher();
WeakReference wref = new WeakReference(new Target(publisher));
GC.Collect();
publisher.Raise();
Assert.False(wref.IsAlive);
}
}