Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to tell if a .NET virtual method has been overriden in a derived class?
    text
    copied!<p>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.</p> <p>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:</p> <pre><code>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() { } } </code></pre> <p>The derived class can now override these virtual methods to provide some custom logic.</p> <p><strong>Here is the question</strong>: 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, <em>before</em> invoking the virtual method?</p> <p>(If it was sufficient to know the answer to this question <em>after</em> 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 <em>not</em> overridden. However this might be fooled if the derived class invokes <code>base.PreValidate()</code> in its overridden implementation.)</p> <p>Perhaps the best solution would be to use a completely different extensibility mechanism if this level of flexibility is required?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload