InterceptionBehaviorAdapter.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Unity.Interception.InterceptionBehaviors;
  5. using Unity.Interception.PolicyInjection.Pipeline;
  6. namespace VCommon.Ioc
  7. {
  8. internal class InterceptionBehaviorAdapter<TInterceptor> : IInterceptionBehavior where TInterceptor : IInterceptor
  9. {
  10. private readonly IInterceptor _interceptor;
  11. public InterceptionBehaviorAdapter(TInterceptor interceptor)
  12. {
  13. _interceptor = interceptor;
  14. }
  15. public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
  16. {
  17. var svcClass = input.Target.GetType();
  18. var svcMethod = input.MethodBase;
  19. IReadOnlyDictionary<string, object> paramDic = Enumerable.Range(0, input.Arguments.Count).ToDictionary(p => input.Arguments.ParameterName(p), p => input.Arguments[p]);
  20. _interceptor.BeforeInvoke(svcClass, svcMethod, paramDic);
  21. var result = getNext()(input, getNext);
  22. _interceptor.AfterInvoke(svcClass, svcMethod, paramDic, result.ReturnValue, result.Exception);
  23. return result;
  24. }
  25. public IEnumerable<Type> GetRequiredInterfaces() => Array.Empty<Type>();
  26. public bool WillExecute => true;
  27. }
  28. }