You can make virtual B invoke the default delegate that you select. The following will allow you to inherit and override: (actually overriding the already overridden: D)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Globalization; using System.Runtime.InteropServices; namespace ConsoleApplication1 { class A { public delegate void delegateB(); public delegateB _B; public A() { _B = B; } public void Override(delegateB newB) { _B = newB; } public virtual void B() { if (_B != null && _B != this.B) { Console.WriteLine("OVERRIDEN B IN A"); _B(); } else { Console.WriteLine("VIRTUAL B IN A"); } } } class cB : A { public override void B() { if (base._B != null && base._B != this.B) { Console.WriteLine("OVERRIDEN B IN B"); _B(); } else { Console.WriteLine("IN B"); } } } class Program { class Overrider { public void MyB() { Console.WriteLine("MyB"); } } public static void Main(string[] Args) { A a = new A(); aB(); Overrider ovr = new Overrider(); a.Override(ovr.MyB); aB();
Output:
VIRTUAL B IN A OVERRIDEN B IN A MyB IN B OVERRIDEN B IN B MyB
You can even call base.B(); in cB.B (), which will effectively call the delegate overriden, but give this result:
VIRTUAL B IN A OVERRIDEN B IN A MyB IN B OVERRIDEN B IN B OVERRIDEN B IN A MyB
I suggest you use a similar approach or approach to design patterns and not even think about Emit reflection. You will not be suitable for use in high-performance applications. Delegates are fast, SLOW reflection.
source share