Call interception method

Is there a way to intercept method calls without any code changes around the method itself?

I do not need to introduce any custom runtime behavior, add my own performance logs to an existing project.

+4
source share
6 answers

You want aspect-oriented programming.

There are 4 basic aromas of AOP

  • Runtime RealProxyaop
  • Subroutine Subclass / AOP Virtual Method
  • Post Compile IL weave AOP
  • Precompile AOP source code

( ). , "" . , , IL, Post Compile IL.

, ,

  • Unity
  • Spring.NET
  • Castle.Windsor

, , , Entity Framework ( Lazy-, ).

, . , dll , , .

  • Postharp Pro
  • Mono.Cecil
  • Fody ( mono.cecil)

. , - .. . #. ... ... .

, , Fody. , , ( Postsharp Pro), nuget Fody.MethodTimer.

+13

AOP-, Spring.NET Unity, . , .

+2

Castle DynamicProxy . , AOP, IoC.

+2

PostSharp

[Serializable]
public class LogPerformance : OnMethodBoundaryAspect
{
    [NonSerialized]
    Stopwatch _stopWatch;

    public override void OnEntry(MethodExecutionArgs args)
    {
        _stopWatch = Stopwatch.StartNew();
        base.OnEntry(args);
    }

    public override void OnExit(PostSharp.Aspects.MethodExecutionArgs args)
    {
        Console.WriteLine(string.Format("[{0}] took {1} ms to execute",
          new StackTrace().GetFrame(1).GetMethod().Name,
            _StopWatch.ElapsedMilliseconds));
        base.OnExit(args);
    }
}

, , :

[LogPerformance]
static void LongRunningCalc()
{
    //Your Code goes here
}

: http://www.codeproject.com/Articles/337564/Aspect-Oriented-Programming-Using-Csharp-and-PostS

+2

, , Fody: https://github.com/fody

, . , . nuget.

:

Requirements, examples, and documents can be found on the fodys github pages .

0
source

All Articles