How can I call the 'base implementation' of an overridden virtual method? [duplicate]
Solution 1:
Using the C# language constructs, you cannot explicitly call the base function from outside the scope of A
or B
. If you really need to do that, then there is a flaw in your design - i.e. that function shouldn't be virtual to begin with, or part of the base function should be extracted to a separate non-virtual function.
You can from inside B.X however call A.X
class B : A
{
override void X() {
base.X();
Console.WriteLine("y");
}
}
But that's something else.
As Sasha Truf points out in this answer, you can do it through IL. You can probably also accomplish it through reflection, as mhand points out in the comments.
Solution 2:
You can't do it by C#, but you can edit MSIL.
IL code of your Main method:
.method private hidebysig static void Main() cil managed
{
.entrypoint
.maxstack 1
.locals init (
[0] class MsilEditing.A a)
L_0000: nop
L_0001: newobj instance void MsilEditing.B::.ctor()
L_0006: stloc.0
L_0007: ldloc.0
L_0008: callvirt instance void MsilEditing.A::X()
L_000d: nop
L_000e: ret
}
You should change opcode in L_0008 from callvirt to call
L_0008: call instance void MsilEditing.A::X()
Solution 3:
You can't, and you shouldn't. That's what polymorphism is for, so that each object has its own way of doing some "base" things.
Solution 4:
You can do it, but not at the point you've specified. Within the context of B
, you may invoke A.X()
by calling base.X()
.
Solution 5:
It's impossible if the method is declared in the derived class as overrides
. to do that, the method in the derived class should be declared as new
:
public class Base {
public virtual string X() {
return "Base";
}
}
public class Derived1 : Base
{
public new string X()
{
return "Derived 1";
}
}
public class Derived2 : Base
{
public override string X() {
return "Derived 2";
}
}
Derived1 a = new Derived1();
Base b = new Derived1();
Base c = new Derived2();
a.X(); // returns Derived 1
b.X(); // returns Base
c.X(); // returns Derived 2
See fiddle here