9

I have a c#-class which provides virtual operations. For each operation exists a synchronous and an asynchronous version.

public class Foo{
   public virtual void Bar(){..};
   public virtual Task BarAsync(){..};

   ...
}

I would like to have the compiler showing a warning if only one version of the operation is overridden (the synchronous or the asynchronous version of the operation) such as the compiler warns when one overrides Equals without overriding GetHashCode or vice versa.

Questioned more broadly: Is it possible to enforce that overriding one method or property enforces the overriding of other properties or methods (via compiler warnings).

3
  • You would have to write a roslyn analyzer. May 30, 2016 at 11:00
  • As HimBromBeere answered, it seems more logical then to just make an abstract base class and put whatever code you actually have that would be overridden in your case here into an existing implementation.
    – Nyerguds
    May 30, 2016 at 11:25
  • The class is a part of a little framework and the proposed architecture complicates it's usage because consuming Foo is not done the same way as derive from Foo.
    – HCL
    May 30, 2016 at 11:30

3 Answers 3

3

Although this is not an answer to your actual question I´m asking for an approach where you do not even need the warning.

Why not create one abstract classes with overridable members and one sealed without:

public class Foo{
   public virtual void Bar(){..}
   public virtual Task BarAsync(){..}
}

public abstract class ImplementIt : Foo {
   public abstract override void Bar();
   public abstract override Task BarAsync();
}
public sealed class DoNotImplementIt : Foo {
   public override void Bar() {..}
   public override Task BarAsync() {..}
}

Now client can design if he needs an implementation of Foo whith your default behaviour (= DoNotImplementIt) or if he needs a customizable version using ImplementIt. In the former case he is forced to override the members in the latter case not.

This approach is far cleaner to your API-user as he knows what to override from the inheritance-chain instead of relying on messy warnings which no-one actually even takes care of.

An even better approach would be to define Foo as an interface which both ImplementIt and DoNotImplementIt implement (sounds weird, however you get it). Saves you from this abstract override. This way you can also hide your interface from the outside by making it internal and only make make the implementing classes accessable from the API.

3
  • You mean the abstract class derives from the first class? There are problems with accesibility (public), and with naming (Bar cannot be a method of a class Bar). May 30, 2016 at 11:16
  • @JeppeStigNielsen Yeap, I ment overriding Foo-class. I also changed the members names. May 30, 2016 at 11:18
  • That helped, but you still have a problem with the derived class Bar being public while its base class is not. May 30, 2016 at 11:21
1

Write code which has the same effect as enforcement.

  • Make an interface containing the methods/properties you want to override.
  • Write one (overridable) function which returns the interface.
  • So when that is overridden, all the functions are replaced at once.

Example:

public interface IBarMethods
{
    void Bar();
    Task BarAsync();
}

public class Foo
{
    public virtual IBarMethods DeMethods()
    {
        // return class containing default methods.
    }
}

public class ImplementIt : Foo
{
    public override IBarMethods DeMethods()
    {
        // return different methods.
    }    
}
1
  • 1
    When doing this Foo can even have a (sealed) method Bar and a (sealed) method BarAsync that just get this IBarMethod object and calls the appropriate method. Of note though I'd probably go with a property, rather than a method, to get the IBarMethods instance (a protected one in my case).
    – Servy
    Jan 16, 2017 at 17:00
0

You could write a custom Code Analyzer. I've never used it myself. We started using FxCop and have written a few custom rules for it. But it's quite hard to do. With Roslyn, this should be much easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.