使用反射来覆盖在C#中的虚方法表 [英] Using reflection to override virtual method tables in C#

查看:142
本文介绍了使用反射来覆盖在C#中的虚方法表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来改变在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.

这篇关于使用反射来覆盖在C#中的虚方法表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆