我需要擴展實例的行爲,但我無法訪問該實例的原始源代碼。例如:C#在運行時更改類方法
/* 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上的原始方法,除非將其轉換爲ABChild。 – RationalGeek 2010-05-03 16:44:54