C# Force re-implement method for inherited class

Hi all,

Just wondering if there is a way to force a method to need to be re-implemented/re-declared if the class is inherited.

Eg.
Base class has method Save, which works fine (not abstract)
Derived class must declare method Save again (because the method will definately be wrong if used from base)

Would protected or similar work here?

Bonus points if the base method is still accessible in the inherited class.

Cheers,
Primoz

Nope. The only way to require the subclass to have something is to have an abstract method.

I’m guessing that your design is a bit off. What’s the problem you’re trying to solve?

Thanks, i figured as much but was hoping maybe not the case.

I’m trying to include an Import/Export feature to my class (eg. serialize). If you derive from the class, you would need to redo the import/export to handle the new additions, such as new fields/data.

You mean ‘yes’ then? :stuck_out_tongue:

Or is it because when the abstract base is implemented, it won’t force the subclass to need an implementation?

Only abstract and interface members get implementation enforcement, and only abstract classes can contain abstract methods.

Since the OP mentions in brackets that his Base class is not abstract, it cannot contain any abstract methods. So, there is no way to enforce overriding any methods.

The design might be able to support an abstract class, but we would need more information before that could be determined.

Regarding the import / export feature, this sounds like serialization, which is commonly considered separate enough form the responsibilities of a given class that it should be handled by another class. Unity provides some built-in support for this, though I understand there are a number of limitations.

One use case I’ve across for needing this:

I am writing a data caching service which stores data in local filesystem and syncs to remote.

Using a serializable abstract base class that all other classes wishing to use this system is ideal and working well, except in one regard:

I’m using JSON.NET (Newtonsoft package, now included with unity) to de/serialize these objects.
The attributes [OnSerializing] and [OnDeserialized] are not valid on abstract / virtual / override methods. So each new class must define these explicitly without relying on an abstract or virtual definition in the base class.

It’s not the worst, but it does require that anyone who has a need to implement these classes know ahead of time that this is a requirement, with no help from the IDE with compiler errors.

My first thought would be just to make the base implementation of the class throw an exception. That way if anyone uses it, it gives a sensible error message. If you really want to get messy you can use reflection to check if you are running the base class or an inherited version.

For a compile time error you want to automatically run a test that uses reflection to check that each class that implements you base class has the proper attributes on the proper methods. You might be able to hijack a unit testing system to do this check.

Neither of these solutions are very pretty.

What about

[OnSerializing]
private void OnSerialize_base() {
    OnSerialize();
}

protected abstract void OnSerialize();

Throw in a comment about the code being silly because the package you’re using is silly, and you’re pretty much done?

2 Likes