This question already has an answer here:
Given the following code, is there a way I can call class A's version of method X?
class A
{
virtual void X() { Console.WriteLine("x"); }
}
class B : A
{
override void X() { Console.WriteLine("y"); }
}
class Program
{
static void Main()
{
A b = new B();
// Call A.X somehow, not B.X...
}
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.
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()
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.
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()
.
I konow it's history question now. But for other googlers: you could write something like this. But this requires change in base class what makes it useless with external libraries.
class A
{
void protoX() { Console.WriteLine("x"); }
virtual void X() { protoX(); }
}
class B : A
{
override void X() { Console.WriteLine("y"); }
}
class Program
{
static void Main()
{
A b = new B();
// Call A.X somehow, not B.X...
b.protoX();
}
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