It is impossible to have a method that is abstract and virtual.
If possible, you can split your method into “before” and “after” parts:
public void DoWork() { DoBeforeWork(); DoCommonWork(); DoAfterWork(); } protected abstract void DoBeforeWork(); protected abstract void DoAfterWork(); private void DoCommonWork() { ... }
Otherwise, your workaround with a second protected method is a very good idea:
public void DoWork() { DoActualWork(); } protected abstract void DoActualWork();
You can check whether DoCommonWork is actually called in DoWork using a thread-field.
However, I would probably make the method virtual. If the derived class does not want to add anything to the common part, it does not need to:
public virtual void DoWork() { ... }
Again, you can check if the common part was actually called.
dtb
source share