3

We are using Visual Studio 2015 and Resharper Ultimate 2016.3.2.

Is there a way to get an error or warning if someone overrides a virtual method of a base class (C# in our case) but does not call the base class implementation? In most of the cases calling the base class is a must and for that few cases where I really don't want to call the base class I want to be able to disable the error or warning with a comment.

I want to make sure that no one forgets to call the base class and if it is really intended not to call the base class, that one has to write a comment stating why it is not intended here.

class A
{
    public virtual void Something(){...}
}
class B : A
{
    public override void Something()
    {
        base.Something();//everything fine. no error or warning.
        someOtherCode...
    }
}
class C : A
{
    public override void Something()
    {
        <-- generate error or warning here
        someOtherCode...
    }
}
class D : A
{
    public override void Something()
    {
        // ReSharper disable once ...: Reason why I don't want to call the base class
        <-- No error or warning here because of the comment.
        someOtherCode...
    }
}
Andi
  • 169
  • 1
  • 9
  • You could do this with a [Roslyn analyzer](http://stackoverflow.com/questions/37966219/visual-studio-warning-if-base-method-is-not-called) – stuartd Feb 16 '17 at 13:47
  • Hmmm... But that means that I have to put an attribute to each virtual method. I would like the default behavior to generate an error when the call is missing. Because that's the common case. Some Reshaper inspection would be great since we use them anyway. – Andi Feb 16 '17 at 14:19

0 Answers0