C# Changing class method at run-time

Posted by Flavio on Stack Overflow See other posts from Stack Overflow or by Flavio
Published on 2010-05-03T16:40:31Z Indexed on 2010/05/03 16:48 UTC
Read the original article Hit count: 316

Filed under:
|
|

Hi, I need to extend the behavior of an instance, but I don't have access to the original source code of that instance. For example:

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

in my code I would to intercept every call to execute, compute some sutff and then call the original execute, something like

/* 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();
}

Is that possible?

Does anyone have a solution for this problem?

© Stack Overflow or respective owner

Related posts about c#

Related posts about runtime