ActionUtils.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. namespace System
  2. {
  3. public static class ActionUtils
  4. {
  5. /// <summary>
  6. /// Action的安全回调
  7. /// </summary>
  8. /// <param name="action"></param>
  9. public static void SafeInvoke(this Action action)
  10. {
  11. try
  12. {
  13. action?.Invoke();
  14. }
  15. catch (Exception ex)
  16. {
  17. XGame.Log.Exception(ex);
  18. }
  19. }
  20. /// <summary>
  21. /// Action的安全回调
  22. /// </summary>
  23. /// <typeparam name="T"></typeparam>
  24. /// <param name="action"></param>
  25. /// <param name="obj"></param>
  26. public static void SafeInvoke<T>(this Action<T> action, T obj)
  27. {
  28. try
  29. {
  30. action?.Invoke(obj);
  31. }
  32. catch (Exception ex)
  33. {
  34. XGame.Log.Exception(ex);
  35. }
  36. }
  37. /// <summary>
  38. /// Action的安全回调
  39. /// </summary>
  40. /// <typeparam name="T"></typeparam>
  41. /// <typeparam name="U"></typeparam>
  42. /// <param name="action"></param>
  43. /// <param name="argT"></param>
  44. /// <param name="argU"></param>
  45. public static void SafeInvoke<T, U>(this Action<T, U> action, T argT, U argU)
  46. {
  47. try
  48. {
  49. action?.Invoke(argT, argU);
  50. }
  51. catch (Exception ex)
  52. {
  53. XGame.Log.Exception(ex);
  54. }
  55. }
  56. /// <summary>
  57. /// Action的安全回调
  58. /// </summary>
  59. /// <typeparam name="T"></typeparam>
  60. /// <typeparam name="U"></typeparam>
  61. /// <typeparam name="V"></typeparam>
  62. /// <param name="action"></param>
  63. /// <param name="argT"></param>
  64. /// <param name="argV"></param>
  65. public static void SafeInvoke<T, U, V>(this Action<T, U, V> action, T argT, U argU, V argV)
  66. {
  67. try
  68. {
  69. action?.Invoke(argT, argU, argV);
  70. }
  71. catch (Exception ex)
  72. {
  73. XGame.Log.Exception(ex);
  74. }
  75. }
  76. /// <summary>
  77. /// Func的安全回调
  78. /// </summary>
  79. /// <typeparam name="TResult"></typeparam>
  80. /// <param name="func"></param>
  81. /// <returns></returns>
  82. public static TResult SafeInvoke<TResult>(this Func<TResult> func)
  83. {
  84. TResult result = default;
  85. try
  86. {
  87. if (func != null)
  88. result = func.Invoke();
  89. }
  90. catch (Exception ex)
  91. {
  92. XGame.Log.Exception(ex);
  93. }
  94. return result;
  95. }
  96. /// <summary>
  97. /// Func的安全回调
  98. /// </summary>
  99. /// <typeparam name="T"></typeparam>
  100. /// <typeparam name="TResult"></typeparam>
  101. /// <param name="func"></param>
  102. /// <param name="argT"></param>
  103. /// <returns></returns>
  104. public static TResult SafeInvoke<T, TResult>(this Func<T, TResult> func, T argT)
  105. {
  106. TResult result = default;
  107. try
  108. {
  109. if (func != null)
  110. result = func.Invoke(argT);
  111. }
  112. catch (Exception ex)
  113. {
  114. XGame.Log.Exception(ex);
  115. }
  116. return result;
  117. }
  118. }
  119. }