On Fri, 22 Aug 2008 10:23:16 -0700, Dilip <rd*****@lycos.comwrote:
This must be fairly straightforward as I used to do this quite often
in C++ but I seem to be missing something really obvious. How do I
force the code to call Foo.DoThis() inside Foo.CallIntoDoThis(), even
though the current instance is of type Bar (and hence the call always
dispatches to Bar.DoThis())?
The usual pattern would be to require the _override_ to manage it. That
is, in class Bar:
public override void DoThis()
{
Console.WriteLine("Bar.DoThis");
base.DoThis();
}
You could implement the literal behavior you're showing by making
Foo.DoThis() call a non-virtual helper method that the Foo class knows
about
:
public override void DoThis()
{
_DoThisImpl();
}
public void CallDoThis()
{
DoThis();
// how do I force a call to Foo.DoThis() here???
_DoThisImpl();
}
private void _DoThisImpl()
{
Console.WriteLine("Foo.DoThis");
}
But I don't like that pattern much, at least not in this case, because you
could conceivably wind up calling the same method twice (if you have an
instance of Foo, not Bar). That seems likely to be wrong, but of course
only you would know for sure if that's really what you meant to do.
Pete