protected object BaseHandle( object instance, Type type, MethodBase method, Func <object[], object> target, string name, object[] args, Type returnType, Attribute[] triggers) { var eventArgs = new AspectEventArgs { Instance = instance, Type = type, Method = method, Name = name, Args = args, ReturnType = returnType, Triggers = triggers }; var wrappers = triggers.OfType <BaseUniversalWrapperAttribute>().ToArray(); var handler = GetMethodHandler(method, returnType, wrappers); return(handler(target, args, eventArgs)); }
protected override void OnBefore(AspectEventArgs eventArgs) { Console.WriteLine($"OnBefore method {eventArgs.Name}"); }
protected override T OnException <T>(AspectEventArgs eventArgs, Exception exception) { Console.WriteLine($"OnException2 method {eventArgs.Name} --> {exception}"); throw exception; }
protected override void OnAfter(AspectEventArgs eventArgs) { Console.WriteLine($"OnAfter2 method {eventArgs.Name}"); }
protected internal sealed override T WrapSync <T>(Func <object[], T> target, object[] args, AspectEventArgs eventArgs) { OnBefore(eventArgs); try { var result = base.WrapSync(target, args, eventArgs); OnAfter(eventArgs); return(result); } catch (Exception exception) { return(OnException <T>(eventArgs, exception)); } }
protected virtual T OnException <T>(AspectEventArgs eventArgs, Exception exception) { throw exception; }
protected virtual void OnAfter(AspectEventArgs eventArgs) { }
protected virtual void OnBefore(AspectEventArgs eventArgs) { }
protected internal sealed override async Task <T> WrapAsync <T>(Func <object[], Task <T> > target, object[] args, AspectEventArgs eventArgs) { OnBefore(eventArgs); try { var result = await target(args); OnAfter(eventArgs); return(result); } catch (Exception exception) { return(OnException <T>(eventArgs, exception)); } }
protected internal virtual T WrapSync <T>(Func <object[], T> target, object[] args, AspectEventArgs eventArgs) { return(target(args)); }