Search code examples
methodsattributesoverridingc#-6.0rider

C# Attribute or Code Inspection Comment to Encourage or Discourage Call to Base Method from Virtual Method Override


I'm working on a C# project in Unity with Rider.

I sometimes see a base class with an empty virtual method, and then a derived class that overrides that method. The method override has an explicit call to base.MethodName() even though the base method is empty.

public class A
{
    public virtual void Method1() { }

    public virtual void Method2()
    {
        // Important logic performed here!
    }
}

public class B : A
{
    public override void Method1()
    {
        base.Method();

        // Do something else ...
    }

    public override void Method2()
    {
        // Do something here ...
    }
}

When looking at the method in Rider's IL Viewer, the call to the base method is included, even though the method is empty.

Are there any method attributes or code inspection comments in C# or Rider that could:

  1. Generate a compiler or code inspection warning when calling a base method that is empty.

  2. Generate a compiler or code inspection warning when not calling a base method that is not empty.

For example:

public class A
{
    [OmitCallFromOverride]
    public virtual void Method1() { }

    [RequireCallFromOverride]
    public virtual void Method2()
    {
        // Important logic performed here!
    }
}

I can imagine a scenario where multiple derived classes override a method and one or more mistakenly failed to call the base method, which might result in unexpected behavior. Or situations where there are unnecessary calls to an empty base method, which may be wasteful, but unlikely to break anything.

While I'm primarily inquiring about whether such attributes or code inspection comments exist, I am also curious to know of how people might handle these situations, such as simply always calling the base method from an override, keeping important logic out of base virtual methods, or using some other method of communicating whether a base method call is unnecessary or required.


Solution

  • Generate a compiler or code inspection warning when calling a base method that is empty.

    In c#, as far as I know, there is no warning for an empty method. So, I think there is no warning when calling a base method that is empty.
    But you are free to write one for you: Write your first analyzer and code fix

    Generate a compiler or code inspection warning when not calling a base method that is not empty.

    Not in C#, and I think is not a good idea to force a derived class to call a base method. I can understand that in your scenario, it would be great if all your derived classes method call always the base method, but it will be a very uncommon case. And generally when we need tricky (not intuitive) rules, that means our solution is not very clear, or it will be error-prone.

    keeping important logic out of base virtual methods

    If you need A.Method1 to be called, maybe let it as a virtual method is not a good idea. You have a virtual method when you want to give to your derived classes the opportunity to use it OR to overwrite it with a more adapted version.
    I propose you a solution that maybe you can adapt to your scenario.

    abstract class A
    {        
        public abstract void Method1();
    
        public virtual void Method2() { }
    
        public void MustBeCalled() 
        { 
            // Here you can put the logic you had in Method1, you need to execute this code, so this method can't be overwrited.             
        }
    
        public void TemplateMethod()
        {
            Method1();
            MustBeCalled();
            // Do something else ...
        }
    
    }