gpt4 book ai didi

c# - C#中使用反射覆盖虚方法表

转载 作者:太空狗 更新时间:2023-10-30 00:35:04 26 4
gpt4 key购买 nike

有没有办法改变 C# 中的虚拟方法表?比如更改虚拟方法指向的位置?

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
}
}

最佳答案

看看LinFu .

关于 Linfu's author's Blog有一个使用 LinFu.AOP 来拦截和更改对您不直接控制的类方法的调用的示例。

关于c# - C#中使用反射覆盖虚方法表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5848918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com