Extension.Call.cs 975 B

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. using System.Diagnostics;
  3. namespace TBL.CSharp.Base
  4. {
  5. public static partial class Extension
  6. {
  7. /// <summary>
  8. /// 对 <paramref name="action"> </paramref> 的保护性调用
  9. /// <para>能够避免发生异常时中断程序</para>
  10. /// </summary>
  11. /// <param name="action">要执行的过程</param>
  12. /// <param name="exception">执行过程中抛出的异常,如果未抛出将会为 <see langword="null"/></param>
  13. /// <returns>是否执行成功</returns>
  14. public static bool ProtectedInvoke(this Action action, out Exception exception)
  15. {
  16. Debug.Assert(action != null);
  17. try
  18. {
  19. action();
  20. exception = null;
  21. return true;
  22. }
  23. catch (Exception e)
  24. {
  25. exception = e;
  26. return false;
  27. }
  28. }
  29. }
  30. }