姿夏的海角
为钱做事,容易累;为理想做事,能够耐风寒;为兴趣做事,则永不倦怠

1. 在Nuget上安装KingAOP

2. 创建一个新的类

public class Test : IDynamicMetaObjectProvider
    {
        public DynamicMetaObject GetMetaObject(Expression paramter)
        {
            return new AspectWeaver(paramter, this);
        }

        [LogAspec]
        public void Operate()
        {
            Console.WriteLine("Call 'Test类' - ‘Operate方法’");
        }
    }

    public class LogAspec : OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionArgs args)
        {
            Console.WriteLine("OnEntry: Hello KingAOP");
        }

        public override void OnException(MethodExecutionArgs args)
        {
            Console.WriteLine("OnException: Hello KingAOP");
        }

        public override void OnSuccess(MethodExecutionArgs args)
        {
            Console.WriteLine("OnSuccess: Hello KingAOP");
        }

        public override void OnExit(MethodExecutionArgs args)
        {
            Console.WriteLine("OnExit: Hello KingAOP");
        }
    }

  

3 在控制台中调用该类 (实践后发现,如果需要用KingAop进行横向切面则必须在实例化切面的类时用动态类型dynamic接收)

static void Main(string[] args)
        {
            #region kingAOP
            dynamic test = new Test();
            try
            {
                Console.WriteLine("Call Mian ..");
                test.Operate();
                Console.WriteLine("Exit Main ..");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            Console.Read();
            #endregion
        }

  

posted on 2019-04-18 15:39  姿夏的海角  阅读(241)  评论(0编辑  收藏  举报