What can I call the implementation of the base class of the method of an explicitly impotent interface?

I am trying to call an explicitly implemented interface method implemented in a base class, but simply cannot make it work. I agree that this idea is ugly, but I tried every combination that I can think of, but to no avail. In this case, I can change the base class, but thought I was asking a question to satisfy my general curiosity.

Any ideas?

// example interface interface MyInterface { bool DoSomething(); } // BaseClass explicitly implements the interface public class BaseClass : MyInterface { bool MyInterface.DoSomething() { } } // Derived class public class DerivedClass : BaseClass { // Also explicitly implements interface bool MyInterface.DoSomething() { // I wish to call the base class' implementation // of DoSomething here ((MyInterface)(base as BaseClass)).DoSomething(); // does not work - "base not valid in context" } } 
+4
source share
1 answer

You cannot (this is not part of the interface available for subclasses). In this scenario, use something like:

 // base class bool MyInterface.DoSomething() { return DoSomething(); } protected bool DoSomething() {...} 

Then any subclass can call protected DoSomething() or (better):

 protected virtual bool DoSomething() {...} 

Now it can simply override and not repeat the implementation of the interface:

 public class DerivedClass : BaseClass { protected override bool DoSomething() { // changed version, perhaps calling base.DoSomething(); } } 
+7
source

All Articles