Can I use the decorator pattern to wrap a method body?

后端 未结 7 1936
醉话见心
醉话见心 2020-12-10 11:01

I have a bunch of methods with varying signatures. These methods interact with a fragile data connection, so we often use a helper class to perform retries/reconnects, etc.

7条回答
  •  庸人自扰
    2020-12-10 11:29

    So, I just went to a AOP session this weekend, and here's a way to do it with PostSharp:

    [Serializable]
    public class MyAOPThing : MethodInterceptionAspect
    {
        public override void OnInvoke(MethodInterceptionArgs args)
        {
            Console.WriteLine("OnInvoke! before");
            args.Proceed();
            Console.WriteLine("OnInvoke! after");
        }
    }
    

    And then decorate methods with [MyAOPThing]. Easy!

提交回复
热议问题