/// <summary> /// 方法异常拦截 /// </summary> /// <param name="methodinfo"></param> /// <param name="args"></param> private void InvokeException(MethodInfo methodinfo, InterceptorMethodArgs args) { ClearInterceptorActionList(); methodinfo.GetCustomAttributes(typeof(InterceptorMethodExceptionAttibute), true).ToList().ForEach(m => { List <Action <InterceptorMethodArgs> > tmpInterceptorActionList = _InterceptorMethodManager.GetInterceptorMethodList(m.GetType()); if (tmpInterceptorActionList != null && tmpInterceptorActionList.Count > 0) { _InterceptorActionList.AddRange(tmpInterceptorActionList); } }); foreach (Action <InterceptorMethodArgs> interceptorActionItem in _InterceptorActionList) { interceptorActionItem(args); } }
public override void Execute(InterceptorMethodArgs args) { IAppService appinstance = args.InstanceObject as IAppService; appinstance.CurrentUnitOfWork.CurrentUnitOfWork.Commit(); }
public object Invoke(object instance, string name, object[] parameters) { object objres = null; MethodInfo methodinfo = null; InterceptorMethodArgs args = null; Stopwatch stopwatch = null; Func <object, object[], object> methodDel = null; string dynamicProxyMethidFullName = null; MethodInfo excuMethod = null; try { if (instance != null) { Type instanceType = instance.GetType(); methodinfo = instanceType.GetMethod(name); if (methodinfo != null) { if (methodinfo.IsGenericMethod) { Type[] genericArgumentsList = methodinfo.GetGenericArguments().Select(m => m.DeclaringType).ToArray(); excuMethod = methodinfo.MakeGenericMethod(genericArgumentsList); } else { excuMethod = methodinfo; } dynamicProxyMethidFullName = GetDynamicProxyMethidFullName(instanceType, excuMethod); methodDel = _DynamicProxyMethodContainerManager.GetDynamicProxyMethodByDynamicProxyMethodName(dynamicProxyMethidFullName); if (methodDel == null) { methodDel = _DynamicProxyMethodContainerManager.AddDynamicProxyContainer(dynamicProxyMethidFullName, methodinfo); } if (methodDel != null) { stopwatch = new Stopwatch(); stopwatch.Start(); args = new InterceptorMethodArgs { MethodName = name, MethodDateTime = DateTime.Now, MethodParameters = parameters, InstanceObject = instance }; InvokeBegin(methodinfo, args); objres = methodDel(instance, parameters); stopwatch.Stop(); args.MethodExecute = stopwatch.Elapsed.TotalMilliseconds; args.ReturnValue = objres; InvokeEnd(methodinfo, args); } else { //TODO:自定义异常 throw new Exception("No Method"); } } } else { //TODO:自定义异常 throw new Exception("No Instance Object"); } } catch (Exception ex) { if (args != null && methodinfo != null) { args.MethodException = ex; InvokeException(methodinfo, args); } throw ex; } return(objres); }