title: Can a base class determine if a derived class has overridden a virtual member?
body: <p>Here's a simplified version of my class:</p>
  
  <pre><code>public abstract class Task
  {
      private static object LockObject = new object();
  
      protected virtual void UpdateSharedData() { }
      protected virtual void UpdateNonSharedData() { }
  
      public void Method()
      {
         lock(LockObject)
         {
            UpdateSharedData();
         }
         UpdateNonSharedData();
      }
  }
  </code></pre>
  
  <p>I'm trying to hide the locking code from derived classes.  But I only want to obtain the lock if the derived class overrides UpdateSharedData; if it doesn't, I don't want the method to block and wait on all of the other running instances that do update shared data before it updates the non-shared data.</p>
  
  <p>So the (seemingly) obvious thing for Method to do is to check and see if the current instance's implementation of UpdateSharedData has overriden the base class's implementation.  I'm pretty sure this isn't possible without using reflection, and it's probably not desirable to do that.</p>
  
  <p>I've thought of some workarounds to this, but they're all pretty awkward:</p>
  
  <ul>
  <li>Add a protected bool property that the derived class's constructor sets, and check that property to see if a lock is needed.  That's doing a pretty terrible job of hiding the locking code from the derived classes.</li>
  <li>Make the UpdateSharedData method a delegate property, have any derived class set
  the property to a private method in its constructor, and only obtain the lock if the delegate isn't null.  That's better, but it still kind of sucks.</li>
  </ul>