Using reflection to override virtual method tables in C #

Is there a way to change virtual method tables in C #? how is the change that the virtual method points to?

class A { public virtual void B() { Console.WriteLine("B"); } } class Program { public static void MyB(A a) { Console.WriteLine("MyB"); } public static void Main(string[] Args) { A a = new A(); // Do some reflection voodoo to change the virtual methods table here to make B point to MyB aB(); // Will print MyB } } 
+4
source share
4 answers

Take a look at LinFu .

The Linfu user blog has an example of using LinFu.AOP to intercept and modify calls, even for class methods that you do not directly control.

+4
source

You cannot change types with reflection, you can only reflect existing types.

However, you can create new types using Reflection.Emit and related classes, but without recompiling your assembly, aB(); in your example, it will always call AB() .

+2
source

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(); // Will print MyB cB b = new cB(); bB(); b.Override(ovr.MyB); bB(); } } } 

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.

+1
source

Dani

What is your ACTUAL problem? Paint me a picture of where to “break” an existing, proven definition of a class on the fly would be desirable. Forgive my skepticism ... but I have a lot of experience writing scripts, so I will take code that does not generate itself, since it works any day. I even (sort of) hate the IOC for this very reason.

However, if you want to create overriding (or implementing) class definitions "on the fly", then I assume that you will use byte code generation (at least what would you do in Java) ... Here is a forum topic on this topic : http://bellyphant.com/2006/11/csharp_bytecode_generation

You can also generate source code and compile it on the fly. Here's a nice free little utils class to compile C # on the fly: http://www.agilekiwi.com/on_the_fly.htm

Good luck with him anyway ... This is an interesting question.

Greetings. Whale.

+1
source

All Articles