与从事件处理程序调用受保护的方法并覆盖受保护的方法相比,覆盖事件处理程序是一种不好的做法。
如果您覆盖事件处理程序,是否存在任何安全问题或任何副作用。
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();
}
}
}