How to invoke (non virtually) the original implementation of a virtual method?

后端 未结 4 1323
不思量自难忘°
不思量自难忘° 2020-12-18 01:14

I have the following situation:

In a 3rd party library (can not be modified):

class A { public virtual void M() {} }

class B : A { public override v         


        
4条回答
  •  我在风中等你
    2020-12-18 01:32

    you can generate dynamic method to make proxy that use Call (not CallVirt) instruction

            var x = new C();
            var m = typeof (A).GetMethod("M");
            var dm = new DynamicMethod("proxy",  typeof (void), new [] {typeof(C)}, typeof (C));
            var il = dm.GetILGenerator();
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Call, m);
            il.Emit(OpCodes.Ret);
            var action = (Action)dm.CreateDelegate(typeof (Action));
            action(x);
    

提交回复
热议问题