小编Fla*_*vio的帖子

C#在运行时更改类方法

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

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

在我的代码中,我会截取每个执行调用,计算一些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();
}
Run Code Online (Sandbox Code Playgroud)

那可能吗?

有没有人有这个问题的解决方案?

.net c# runtime

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

.net ×1

c# ×1

runtime ×1