Intercept method calls

前端 未结 6 1273
故里飘歌
故里飘歌 2020-12-06 08:30

Is there a way of intercepting some method calls without making any code changes around the method itself?

I don\'t need to inject any custom behaviour at runtime,

相关标签:
6条回答
  • 2020-12-06 08:38

    I have successfully used Castle DynamicProxy for that. It's more lightweight than a full fledged AOP framework, and can be used without an IoC container.

    0 讨论(0)
  • 2020-12-06 08:40

    You want Aspect Oriented Programming.

    There are 4 main flavours of AOP

    1. Runtime RealProxy based AOP
    2. Runtime subclass/virtual method AOP
    3. Post Compile IL weave AOP
    4. Precompile source code AOP

    The above are ordered in order of speed of execution (slowest to fastest). Note the last two "should" be the same speed. However I expect the compiler to produce better IL than a Post Compile IL weave.

    The first camp usually includes IOC containers, since they lend themselves fairly well to this pattern, including and not limited to

    • Unity
    • Spring.NET
    • Castle.Windsor
    • Postsharp Express

    The second camp, is pretty rare, and the only project that I can think of off the top of my head is Entity Framework (which uses it for Lazy loading, however it isn't extensible, and cannot be customised).

    The third camp is pretty sparce also, since this technique is extremely complicated and difficult. This works by editing the dll assembly AFTER you have compiled, to add the extra code you want.

    • Postsharp Pro
    • Mono.Cecil
    • Fody (a mono.cecil wrapper)

    The final camp is relatively new. So new in fact, the only real entry is the experimental MS Roslyn. This is actually a C# compiler. So...yeah...its pretty magic.

    Now, if you are having real huge performance issues with performance critical code, I would suggest using Fody. What is awesome about this, is that it is free (unlike Postsharp Pro), it uses nuget and there is already a performance tool in Fody.MethodTimer.

    0 讨论(0)
  • 2020-12-06 08:47

    What you are looking for is Fody: https://github.com/fody

    Its open source, stable and has lots of plugins for different AOP use cases. I am using it in a huge commercial application, and it is working very well. Installation and configuration is super easy and done within some minutes via nuget.

    Some example plugins are:

    • PropertyChanged: (Injects INotifyPropertyChanged code into properties at compile time)
    • Anotar (Simplifies logging through a static class and some IL manipulation)
    • Method Timer (Injects some very basic method timing code)
    • ... and many more!

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

    0 讨论(0)
  • 2020-12-06 08:51

    You could take a look at Aspect Oriented Programming, and see if it's a solution for your situation.

    For instance: http://docs.castleproject.org/Default.aspx?Page=Introduction-to-AOP-With-Castle&NS=Windsor&AspxAutoDetectCookieSupport=1

    http://fgheysels.blogspot.be/2006/11/aspect-oriented-programming-in-net.html

    0 讨论(0)
  • 2020-12-06 08:59

    Using 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);
        }
    }
    

    Use the aspect like so on a function:

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

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

    0 讨论(0)
  • 2020-12-06 09:00

    You can use any AOP Frameworks, like to Spring .NET or Unity, to intercept calls, before or after the method execute. Thus, you dont need to change your method code.

    0 讨论(0)
提交回复
热议问题