KingAOP拦截方案

Posted wuguangwei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了KingAOP拦截方案相关的知识,希望对你有一定的参考价值。

KingAOP

using KingAOP.Aspects;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using KingAOP;

namespace ConsoleApplication5
{
    /// <summary>
    /// aop日志拦截示例 使用KingAOP
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Login test = new Login();
            dynamic entity = new User { ID = 99, Name = "Jon", Pwd = "wang", State = "", LoginTime = System.DateTime.Now };
            test.LoginValdate(entity);
            Console.Read();
        }
    }
    public class User
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Pwd { get; set; }
        public string State { get; set; }
        public System.DateTime LoginTime { get; set; }
    }

    /// <summary>
    /// 创建一个日志切面处理类
    /// </summary>
    public class LoggingAspect : OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionArgs args)
        {
            string logData = CreateLogData("entering", args);
            Console.WriteLine(logData);
        }

        public override void OnException(MethodExecutionArgs args)
        {
            string logData = CreateLogData("exception", args);
            Console.WriteLine(logData);
        }

        public override void OnSuccess(MethodExecutionArgs args)
        {
            string logData = CreateLogData("success", args);
            Console.WriteLine(logData);
        }

        public override void OnExit(MethodExecutionArgs args)
        {
            string logData = CreateLogData("exiting", args);
            Console.WriteLine(logData);
        }

        /// <summary>
        /// AOP处理登录日志逻辑,只需在此进行修改即可,无需修改被切面的处理类
        /// </summary>
        /// <param name="methodStage"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        private string CreateLogData(string methodStage, MethodExecutionArgs args)
        {
            var str = new StringBuilder();
            str.AppendLine();
            str.AppendLine(string.Format(methodStage + " {0} ", args.Method));
            foreach (var argument in args.Arguments)
            {
                //下面利用反射机制获取对象名称和对象属性和属性值
                var argType = argument.GetType();

                str.Append(argType.Name + ": ");

                if (argType == typeof(string) || argType.IsPrimitive)
                {
                    str.Append(argument);
                }
                else
                {
                    foreach (var property in argType.GetProperties())
                    {
                        str.AppendFormat("{0} = {1}; ",
                            property.Name, property.GetValue(argument, null));
                    }
                }
            }
            return str.ToString();
        }
    }

    /// <summary>
    /// 该类需要继承IDynamicMetaObjectProvider,因为KingAOP是基于动态类型进行操作和绑定的,如不继承是不会进入到刷选器中的相应事件里
    /// 登录功能,只需添加一个LoggingAspect即可实现日志功能,达到业务逻辑和通用处理逻辑的分离
    /// </summary>
    public class Login : IDynamicMetaObjectProvider
    {
        //添加处理日志切面
        [LoggingAspect]
        public void LoginValdate(User entity)
        {
            //只需进行业务逻辑处理,无需进行日志处理,日志处理交给切面处理
            if (entity.Name == "jack" && entity.Pwd == "wang")
            {
                entity.State = "Logged";
            }
            else
            {
                entity.State = "Error";
            }
        }

        /// <summary>
        /// 该类必须实现IDynamicMetaObjectProvider的GetMetaObject方法
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public DynamicMetaObject GetMetaObject(Expression parameter)
        {
            return new AspectWeaver(parameter, this);
        }

      
    }
}

  

以上是关于KingAOP拦截方案的主要内容,如果未能解决你的问题,请参考以下文章

OkHttpInterceptor 从 kotlin 拦截器导航到登录片段

METAL顶点/片段着色器“拦截”结果的最佳方法

ViewPager里面的TextView拦截触摸事件

python 代码片段和解决方案

解决方案电影标题中缺少代码的片段,完成挑战更多[关闭]

跨站攻击防御拦截方案