调用一下试试:
可见,全类方法标签 Interceptor 在 Test 和 GetInt 方法调用前后都打出了对应的日志;
Action方法标签只在 Test 方法上做了标记,那么Test方法 Before 和 After 执行时打出了日志;
【实现过程】
实现的思路在上面已经有详细的讲解,可以参考静态代理的实现思路。
我们定义一个动态代理生成类 DynamicProxy,用于原业务代码的扫描和代理类代码的生成;
定义两个过滤器标签,ActionBaseAttribute,提供Before和After切面方法;InterceptorBaseAttribute,提供 Invoke “全调用”包装的切面方法;
Before可以获取到当前调用的方法和参数列表,After可以获取到当前方法调用以后的结果。
Invoke 可以拿到当前调用的对象和方法名,参数列表。在这里进行反射动态调用。
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class ActionBaseAttribute : Attribute
{
public virtual void Before(string @method, object[] parameters) { }
public virtual object After(string @method, object result) { return result; }
}











