如果在这篇博文中找到 WeakEventHandler
但他说:
更新:是的,我知道这是坏的。有一个错误意味着它不起作用...
并没有说什么是错误:(
public class WeakEventHandler<DT, T> where T : EventArgs
{
private WeakReference weakRefToOriginalDelegate;
public WeakEventHandler(EventHandler<T> originalDelegate)
{
weakRefToOriginalDelegate = new WeakReference(originalDelegate);
}
private void DoInvoke(object sender, T args)
{
EventHandler<T> originalDelegate = (EventHandler<T>) weakRefToOriginalDelegate.Target;
if (originalDelegate != null) originalDelegate(sender, args);
}
public static implicit operator DT(WeakEventHandler<DT, T> wd)
{
object o = Delegate.CreateDelegate(typeof (DT), wd, "DoInvoke");
return (DT) o;
}
}
有人可以解释这个 WeakEventHandler 实现中的错误是什么吗?