【发布时间】:2010-05-03 16:40:31
【问题描述】:
我需要扩展实例的行为,但我无权访问该实例的原始源代码。例如:
/* 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();
}
这可能吗?
有人有解决这个问题的办法吗?
【问题讨论】: