Do.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using SmartCoalApplication.Base;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace SmartCoalApplication
  8. {
  9. public static class Do
  10. {
  11. public static void Test<T>(
  12. T value,
  13. Function<bool, T> testFn,
  14. Procedure<T> ifTrueFn,
  15. Procedure<T> ifFalseFn)
  16. {
  17. (testFn(value) ? ifTrueFn : ifFalseFn)(value);
  18. }
  19. public static void GenerateTest<T>(
  20. Function<T> generate,
  21. Function<bool, T> test,
  22. Procedure<T> ifTrue,
  23. Procedure<T> ifFalse)
  24. {
  25. Test(generate(), test, ifTrue, ifFalse);
  26. }
  27. public static bool TryBool(Procedure actionProcedure)
  28. {
  29. try
  30. {
  31. actionProcedure();
  32. return true;
  33. }
  34. catch (Exception)
  35. {
  36. return false;
  37. }
  38. }
  39. public static T TryCatch<T>(
  40. Function<T> actionFunction,
  41. Function<T, Exception> catchClause)
  42. {
  43. T returnVal;
  44. try
  45. {
  46. returnVal = actionFunction();
  47. }
  48. catch (Exception ex)
  49. {
  50. returnVal = catchClause(ex);
  51. }
  52. return returnVal;
  53. }
  54. }
  55. }