/// <summary> /// 根据接口创建代理类,并经过注入处理。 /// 如果无法创建接口代理,则返回未经代理,但注入过的对象。 /// </summary> /// <param name="targetType">需要代理的类型</param> /// <param name="interfaceType">接口类型</param> /// <returns></returns> public static Object Create(Type targetType, Type interfaceType) { Object objTarget = AopContext.CreateObjectByInterface(targetType, interfaceType); Inject(objTarget); return(objTarget); }
public virtual void OnResult(AopContext context, object returnValue) { var starttime = (DateTime)context.UserState; var time = DateTime.Now - starttime; LogUtils.Debug($"【AOP】Class: {context.TargetType.FullName}, Method: {context.Method.Name}, TotalMilliseconds: {time.TotalMilliseconds}"); }
/// <summary> /// 创建一个经过Aop和Ioc处理的对象,结果不是单例。 /// 如果需要拦截,则创建代理子类;然后检测是否需要注入。 /// </summary> /// <param name="targetType"></param> /// <returns></returns> public static Object CreateObject(Type targetType) { Object objTarget = AopContext.CreateObjectBySub(targetType); Inject(objTarget); return(objTarget); }
public virtual void OnException(AopContext context, Exception ex) { StringBuilder msg = new StringBuilder(); msg.AppendFormat("【AopLog.OnException】Class: {0}, Method: {1};", context.TargetType.FullName, context.Method.Name); var param = context.Method.GetParameters(); object[] args = context.Arguments ?? new object[0]; for (int i = 0; i < param.Length; i++) { var p = param[i]; if (!p.IsOut && !typeof(Delegate).IsAssignableFrom(p.ParameterType)) { msg.AppendFormat("\r\n{0}: {1}", p.Name, args.Length > i ? (args[i] == null ? "null" : JsonUtils.Serialize(args[i])) : ""); } } msg.Append("异常:"); if (ex is MsgStatusException) { LogUtils.Info(msg.ToString(), ex); } else { LogUtils.Error(msg.ToString(), ex); } }
public void testSubclassObject() { // 不管有没有监控,CreateObject 都返回结果 MyNormalService s3 = AopContext.CreateObjectBySub <MyNormalService>(); Assert.IsNotNull(s3); Assert.AreEqual(typeof(MyNormalService), s3.GetType()); }
/// <summary> /// 用Aop拦截所有属性对象。 /// 如果属性有接口,按照接口拦截;否则按照子类拦截 /// </summary> /// <param name="objTarget"></param> public static Object InterceptProperty(Object objTarget) { Type targetType = objTarget.GetType(); PropertyInfo[] properties = targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo p in properties) { if (!p.CanRead) { continue; } if (!p.CanWrite) { continue; } if (rft.IsBaseType(p.PropertyType)) { continue; } Object oValue = p.GetValue(objTarget, null); if (oValue == null) { logger.Info("property value is null. type = " + targetType.FullName + ", property = " + p.Name); continue; } Type pType = oValue.GetType(); Object propertyValue = oValue; if (p.PropertyType.IsInterface) { if (AopContext.CanCreateInterfaceProxy(pType, p.PropertyType)) { propertyValue = AopContext.createProxyByInterface(pType, p.PropertyType); if (propertyValue != null) { p.SetValue(objTarget, propertyValue, null); } } } else if (AopContext.CanCreateSubProxy(pType)) { propertyValue = AopContext.createProxyBySub(pType); if (propertyValue != null) { p.SetValue(objTarget, propertyValue, null); } } } return(objTarget); }
/// <summary> /// 创建一个经过Aop和Ioc处理的对象,结果不是单例。 /// 如果需要拦截,则创建代理子类;然后检测是否需要注入。 /// </summary> /// <param name="targetType"></param> /// <param name="invokerName">如果是根据接口自动装配,</param> /// <returns></returns> private static Object CreateObject(Type targetType, Object invoker) { if (targetType == null) { return(null); } if (targetType.IsInterface) { return(CreateByInterface(targetType, invoker)); } Object objTarget = AopContext.CreateObjectBySub(targetType); Inject(objTarget); return(objTarget); }
public void testSubClassProxy() { // 代理类是子类 MyAopService s1 = AopContext.CreateProxyBySub <MyAopService>(); Boolean isSubClass = s1.GetType().IsSubclassOf(typeof(MyAopService)); Assert.IsTrue(isSubClass); Assert.AreEqual("__" + typeof(MyAopService).Name, s1.GetType().Name); Assert.AreNotEqual(typeof(MyAopService), s1.GetType()); s1.Save(); Console.WriteLine(); Console.WriteLine(); // 未修改结果 int result1 = s1.Update(88); Assert.AreEqual(MyAopService.UpdateResult, result1); Console.WriteLine(); Console.WriteLine(); s1.GetBy("myname", 3); Console.WriteLine(); Console.WriteLine(); // 边界测试:null List <String> result2 = s1.GetCat(); Assert.IsNull(result2); Console.WriteLine(); Console.WriteLine(); // 泛型列表 List <String> result3 = s1.GetDog(); Assert.IsNotNull(result3); Assert.AreEqual(0, result3.Count); Console.WriteLine(); Console.WriteLine(); // 后置修改:修改结果数据 List <MyCat> result4 = s1.GetCat2(); Assert.IsNotNull(result4); Assert.AreEqual(4, result4.Count); Assert.AreEqual("cat999", result4[3].Name); Console.WriteLine(); Console.WriteLine(); // 未监控的方法 s1.NormalMethod(); Console.WriteLine(); Console.WriteLine(); // 未监控的虚方法 s1.NormalVirtualMethod(); Console.WriteLine(); Console.WriteLine(); // 没有监控的对象,则没有代理类 MyNormalService s2 = AopContext.CreateProxyBySub <MyNormalService>(); Assert.IsNull(s2); // 测试参数是否成功修改 MyArgService x1 = AopContext.CreateProxyBySub <MyArgService>(); Console.WriteLine("buy1"); int xResult1 = x1.Buy(3); Console.WriteLine(); Console.WriteLine("buy2"); int xResult2 = x1.Buy(5); Assert.AreEqual(8, xResult1); Assert.AreEqual(12, xResult2); }
public virtual void OnExecuting(AopContext context) { context.UserState = DateTime.Now; }