使用反射来覆盖在C#中的虚方法表射来、方法

2023-09-04 02:51:33 作者:等候、弎年

有没有办法来改变在C#中的虚方法表?样改变,其中一个虚方法指向?

  A级
{
    公共虚拟无效B()
    {
        Console.WriteLine(B);
    }
}
类节目
{
    公共静态无效的MYB(A一)
    {
        Console.WriteLine(MYB);
    }
    公共静态无效的主要(字串[] args)
    {
        A中的=新的A();
        //做一些反思巫术改变这里的虚方法表,以点B以MYB
        A·B(); //将打印MYB
    }
}
 

解决方案

看看李林甫。

在李林甫的作者的博客有一个例子使用LinFu.AOP拦截和变化要求甚至到了类的方法,你不直接控制。

内存分页

Is there a way to change the virtual methods tables in C#? like change where a virtual method is pointing?

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
        a.B(); // Will print MyB
    }
}

解决方案

Take a look at LinFu.

On Linfu's author's Blog there's an example of using LinFu.AOP to intercept and change calls even to methods of classes that you don't control directly.

 
精彩推荐
图片推荐