我需要扩展一个实例的行为,但我无法访问该实例的原始源代码。例如:
/* I don't have the source code for this class, only the runtime instance */
Class AB
{
public void execute();
}
在我的代码中,我想拦截每个调用执行的操作,进行一些处理后再调用原始的执行操作,类似于:
/* 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();
}
这可行吗?
有没有人能提供解决这个问题的方案?
AB
的第三方代码将继续使用原始类。 - ivan_pozdeev