1

I know this is a bad idea, but I have a base class with a lot of virtual methods A(), B(), C() and a set of matching getters bool SupportsA(), bool supportsB(), ...

The idea is that implementers will only want to provide functionality for a small minority of virtual methods and I'd like to relieve them of the responsibility to also override the associated getter. Is there a way to tell from the base class whether any of the (potentially more than one) subclasses has overridden a specific method? Note these classes may be defined in different assemblies so I do not know the Type.

public class
{
  public bool SupportsA
  {
    get { return (A == overridden); }
  }
  public virtual void A()
  {
    // Null default implementation.
  }
}

There are some similar questions already, but they seem to assume that both the base type and the derived type are known at compile time.

David Rutten
  • 4,716
  • 6
  • 43
  • 72

1 Answers1

1
public class BaseClass
 {
    public bool SupportsA
    {
        get { return (this.GetType().GetMethod("A").DeclaringType == typeof(BaseClass)); }
    }
    public virtual void A()
    {
        // Null default implementation.
    }
}
George Lica
  • 1,798
  • 1
  • 12
  • 23