Use reflection to invoke an overridden base method

How to use reflection call a base method that is overridden by derived class?

class Base
{
    public virtual void Foo() { Console.WriteLine("Base"); }
}
class Derived : Base
{
    public override void Foo() { Console.WriteLine("Derived"); }
}
public static void Main()
{
    Derived d = new Derived();
    typeof(Base).GetMethod("Foo").Invoke(d, null);
    Console.ReadLine();
}

This code always shows 'Derived'...


After a long time, I finally find a better solution than DynamicMethod:

class CallOverride
{
    public static void Test()
    {
        var obj = new Override();
        var method = typeof(object).GetMethod("ToString");
        var ftn = method.MethodHandle.GetFunctionPointer();
        var func = (Func<string>)Activator.CreateInstance(typeof(Func<string>), obj, ftn);
        Console.WriteLine(func());
    }
}

class Override
{
    public override string ToString()
    {
        return "Nope";
    }
}

This solution use the standard constructor signature of delegate:

public Delegate(object target, IntPtr ftn)

where target is the target instance and ftn is the function pointer. It directly invoke it with the function pointer of base method, so the delegate will point to the actual base method, not the overridden method.


Even though the current answer is already accepted, it's actually possible without having to change the original class by using a dynamic method like this:

    static void Main(string[] args)
    {
        Derived foo = new Derived();
        foo.Foo();

        MethodInfo method = typeof(Base).GetMethod("Foo");
        DynamicMethod dm = new DynamicMethod("BaseFoo", null, new Type[] { typeof(Derived) }, typeof(Derived));
        ILGenerator gen = dm.GetILGenerator();
        gen.Emit(OpCodes.Ldarg_1);
        gen.Emit(OpCodes.Call, method);
        gen.Emit(OpCodes.Ret);

        var BaseFoo = (Action<Derived>)dm.CreateDelegate(typeof(Action<Derived>));
        BaseFoo(foo);

        Console.ReadKey();
    }

as you can see it's still relatively simple to do


This can be achieved through Code emit

http://blogs.msdn.com/b/rmbyers/archive/2008/08/16/invoking-a-virtual-method-non-virtually.aspx


You can't do that, even with reflection. Polymorphism in C# actually guarantees that Derived.Foo() will always be called, even on an instance of Derived cast back to its base class.

The only way to call Base.Foo() from a Derived instance is to explicitly make it accessible from the Derived class:

class Derived : Base
{
    public override void Foo()
    {
        Console.WriteLine("Derived");
    }

    public void BaseFoo()
    {
        base.Foo();
    }
}

Here is a general form of @Kii answer above:

// returns a delegate (of type methodType) that calls obj.base.method(..)
static object VirtualMethodBase(Type methodType, string methodName, Object obj) {
  var method = obj.GetType().BaseType.GetMethod(methodName);
  var ftn = method.MethodHandle.GetFunctionPointer();
  return Activator.CreateInstance(methodType, obj, ftn);
}

usage:

public class Base {
  public override string ToString() => "Base";
  public virtual int F(int x, int y) => x+y; 
}

public class Derived : Base {
  public override string ToString() => "Derived";
  public override int F(int x, int y) => x*y; 
}

var b = new Base();
var d = new Derived();

var sb = b.ToString(); // "Base"
var fb = b.F(2, 3); // 5

var sd = d.ToString(); // "Derived"
var fd = d.F(2, 3); // 6

// obj.base.ToString()
static string ToStringBase(object obj) => ((Func<string>)VirtualMethodBase(typeof(Func<string>), "ToString", obj))();

// obj.base.F(x, y)
static int FBase(Base bobj, int x, int y) => ((Func<int, int, int>)VirtualMethodBase(typeof(Func<int, int, int>), "F", bobj))(x, y);

var sd1 = ToStringBase(d); // "Base"
var fd1 = FBase(d, 2, 3); // 5