與從Event Handler調用受保護的方法並覆蓋受保護的方法相比,覆蓋事件處理程序是不好的做法。正在覆蓋事件處理程序不良實踐
這些是否是任何安全問題,或者是否覆蓋事件處理程序的任何副作用。
namespace Testing
{
public class Owner
{
public event EventHandler<EventArgs> OnAction;
}
public class Foo
{
public Foo(Owner owner)
{
owner.OnAction += OnAction;
owner.OnAction += OnAction2;
}
protected virtual void OnAction(object sender, EventArgs eventArgs)
{
}
private void OnAction2(object sender, EventArgs eventArgs)
{
ProtectedMethod();
}
protected virtual void ProtectedMethod()
{
}
}
public class FooDerived: Foo
{
public FooDerived(Owner owner) : base(owner)
{
}
protected override void OnAction(object sender, EventArgs eventArgs)
{
// Do Something
base.OnAction(sender, eventArgs);
}
}
public class FooDrived2: Foo
{
public FooDrived2(Owner owner) : base(owner)
{
}
protected override void ProtectedMethod()
{
// Do something
base.ProtectedMethod();
}
}
}
謝謝,你知道任何事件處理程序被重寫.Net框架 – skjagini
@SujithJagini:我不能說我這樣做。這不是經常出現的東西,通常如果它涉及多個行爲,它最終會分解成多種接口方法。 –