Is there a way in .NET to have a method called automatically after calling another method, but before entering it

I am looking for a way to call a method after calling another method, but before entering it. Example:

public class Test {

  public void Tracer ( ... )
  {
  }

  public int SomeFunction( string str )
  {
    return 0;
  }

  public void TestFun()
  {
    SomeFunction( "" );
  }

}

In the above example, I would like to call Tracer () after the function SomeFunction () was called TestFun (), but before SomeFunction () was introduced. I would also like to get reflection data in SomeFunction ().


- . - Castle DynamicProxy; , , , . , "", "" , , Dynamic Proxy. .

, AOP ContextBoundObject .

+5
8

(Castle DynamicProxy), , , , , .

+5

* Core:

public int SomeFunction(string str)
{
    Tracer();
    return SomeFunctionCore(str);
}

private int SomeFunctionCore(string str)
{
    return 0;
}

API- .NET ( WPF).

+2

!

delegate void SomeFunctionDelegate(string s);

void Start()
{
  TraceAndThenCallMethod(SomeFunction, "hoho");
}

void SomeFunction(string str)
{
  //Do stuff with str
}

void TraceAndThenCallMethod(SomeFunctionDelegate sfd, string parameter)
{
  Trace();
  sfd(parameter);
}
+2

Aspect Oriented Programming. , AOP .NET: http://www.postsharp.org/aop.net/

- " " . - () . ? , . AOP - , , . , , , , AOP .

+1

.NET ContextBoundObject, , . , , .

+1
0

(.. ), , .NET Profiling API. , COM- , , .

0

, . () , , , , . , , .

What happens, we just get a second class, which is sometimes or always created instead of its parent. The methods that we want to track (or otherwise track) are declared virtual and redefined in the derived class to perform any actions that we want to track, and then the function is called in the parent class.

public class TestClass {

  public virtual void int SomeFunction( string /*str*/ )
  {
    return 0;
  }


  public void TestFun()
  {
    SomeFunction( "" );
  }

}


public class TestClassTracer : TestClass {

  public override void int SomeFunction( string str )
  {
    // do something
    return base.SomeFunction( str );
  }

}
0
source

All Articles