AopHelper.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. 
  2. using System;
  3. using System.Reflection;
  4. using System.Threading.Tasks;
  5. using Castle.DynamicProxy;
  6. namespace Admin.Core.Aop
  7. {
  8. public class AopHelper
  9. {
  10. public static async Task<T> ExecuteGenericMethod<T>(Task<T> returnValue, Action<T> callBackAction, Action<Exception> exceptionAction)
  11. {
  12. try
  13. {
  14. var result = await returnValue;
  15. callBackAction?.Invoke(result);
  16. return result;
  17. }
  18. catch (Exception ex)
  19. {
  20. exceptionAction?.Invoke(ex);
  21. throw;
  22. }
  23. }
  24. public static object CallGenericMethod(IInvocation invocation, Action<object> callBackAction, Action<Exception> exceptionAction)
  25. {
  26. return typeof(AopHelper)
  27. .GetMethod("ExecuteGenericMethod", BindingFlags.Public | BindingFlags.Static)
  28. .MakeGenericMethod(invocation.Method.ReturnType.GenericTypeArguments[0])
  29. .Invoke(null, new object[] { invocation.ReturnValue, callBackAction, exceptionAction });
  30. }
  31. }
  32. }