Say you have access to the MyClass base class, which implements "IFoo". "IFoo" defines the function "int FooValue ()", and "MyClass" implements it explicitly. Now say that you have a subclass of “MyClass” called “MySubClass” and you want to override FooValue in that subclass, but also want the implementation of the subclass to be based on the result of the base class implementation.
Now, as a rule, this can be solved by simply translating the implementation into a protected function in the base class, which we simply pass into a subclass. Done and done. But we do not have access to the source code of the base class. We only have a link to the library. So how do you solve this?
Do not duplicate (update: ... like this one)!
Here is this SO question here ... C #: redefining properties by explicitly specifying an interface ... which shows that you cannot redefine the base class interface through regular channels as such, you can explicitly reimplement the same interface in subclass and which behaves as if you are redefining the interface (but in fact you are reimplementing it, not redefining it). This suggests that I am trying to figure out how I can get the base class implementation. (This is why IMHO is not a duplicate of this question.)
Here is some pseudo-code of the base class, which again, we do not have access to the code ...
public interface IFoo { int FooValue(); } public class MyClass : IFoo { int IFoo.FooValue() <-- Explicit implementation requiring a cast to access. { return 4; } }
This is what we are trying to do, but obviously it is not allowed because you cannot use a “base” like this.
public class MySubClass : MyClass { int IFoo.FooValue() { int baseResult = ((IFoo)base).FooValue(); <-- Can't use 'base' like this return baseResult * 2; } }
So is this possible?
source share