C#: Any way to skip over one of the base calls in polymorphism?
Your design is wrong if you need this.
Instead, put the per-class logic in DoFoo
and don't call base.DoFoo
when you don't need to.
class GrandParent
{
public void Foo()
{
// base logic that should always run here:
// ...
this.DoFoo(); // call derived logic
}
protected virtual void DoFoo() { }
}
class Parent : GrandParent
{
protected override void DoFoo()
{
// Do additional work (no need to call base.DoFoo)
}
}
class Child : Parent
{
protected override void DoFoo()
{
// Do additional work (no need to call base.DoFoo)
}
}
I think there is something wrong with your design here. Essentially, you want to "break" the rules of polymorphism. You are saying Child
should derive from Parent
but want to conveniently skip the implementation in it's parent.
Re-think your design.