The very act of asking this question suggests that my approach to this problem is incorrect, so I'm interested in answers which address the question directly, as well as answers which suggest a cleaner approach to what I'm doing.
Consider a base class which provides a standard set of services and functionalities, as well as some structure around those. By way of metaphor, let's consider the following example class:
public class ExampleBase
{
public void Main()
{
// Do something
PreValidate(); // Extensibility point for derived classes
// Do something else
PostValidate(); // Extensibility point for derived classes
// Do something else
}
protected virtual void PreValidate()
{
}
protected virtual void PostValidate()
{
}
}
The derived class can now override these virtual methods to provide some custom logic.
Here is the question: Is it possible for the base class to discover at runtime if the derived class has taken the liberty of overriding one of these virtual methods, before invoking the virtual method?
(If it was sufficient to know the answer to this question after invoking the method, then you could perhaps replace the empty method in the base class with a method that sets a private flag, which would indicate the method was not overridden. However this might be fooled if the derived class invokes base.PreValidate()
in its overridden implementation.)
Perhaps the best solution would be to use a completely different extensibility mechanism if this level of flexibility is required?