12345678910111213141516171819202122232425262728293031323334353637383940 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using Unity.Interception.InterceptionBehaviors;
- using Unity.Interception.PolicyInjection.Pipeline;
- using VCommon.Diagnostics;
- namespace VCommon.Ioc
- {
- public abstract class InterfaceInterceptorBase : IInterceptionBehavior
- {
- public virtual bool WillExecute => true;
- public virtual IEnumerable<Type> GetRequiredInterfaces() => Array.Empty<Type>();
- public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
- {
- long msUsed = 0;
- var begin = DateTime.Now;
- var svcClass = input.Target.GetType();
- var svcMethod = input.MethodBase;
- IReadOnlyDictionary<string, object> paramDic = Enumerable.Range(0, input.Arguments.Count).ToDictionary(p => input.Arguments.ParameterName(p), p => input.Arguments[p]);
- BeforeInvoke(begin, svcClass, svcMethod, paramDic);
- IMethodReturn result;
- using (new TimingMeasureAction(p => msUsed = p)) result = getNext()(input, getNext);
- AfterInvoke(begin, msUsed, svcClass, svcMethod, paramDic, result.ReturnValue, result.Exception);
- return result;
- }
- protected abstract void BeforeInvoke(DateTime time, Type svcClass, MethodBase svcMethod, IReadOnlyDictionary<string, object> paramDic);
- protected abstract void AfterInvoke(DateTime time, long msUsed, Type svcClass, MethodBase svcMethod, IReadOnlyDictionary<string, object> paramDic, object returnValue, Exception exception);
- }
- }
|