gpt4 book ai didi

C# 在运行时更改类方法

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

<分区>

我需要扩展实例的行为,但我无权访问该实例的原始源代码。例如:

/* I don't have the source code for this class, only the runtime instance */
Class AB
{
public void execute();
}

在我的代码中,我会拦截每个执行调用,计算一些 sutff,然后调用原始执行,类似于

/* This is how I would like to modify the method invokation */
SomeType m_OrgExecute;

{
AB a = new AB();
m_OrgExecute = GetByReflection( a.execute );
a.execute = MyExecute;
}

void MyExecute()
{
System.Console.Writeln( "In MyExecute" );
m_OrgExecute();
}

这可能吗?

有人能解决这个问题吗?

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