Events.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. namespace Wayne.Lib
  3. {
  4. /// <summary>
  5. /// Contains extension methods for firing events.
  6. /// </summary>
  7. public static class Events
  8. {
  9. /// <summary>
  10. /// Fires the event if it is not null.
  11. /// </summary>
  12. /// <typeparam name="TEventArgs"></typeparam>
  13. /// <param name="eventHandler"></param>
  14. /// <param name="sender"></param>
  15. /// <param name="eventArgs"></param>
  16. public static void Fire<TEventArgs>(this EventHandler<TEventArgs> eventHandler, object sender, TEventArgs eventArgs)
  17. where TEventArgs : EventArgs
  18. {
  19. if (eventHandler != null)
  20. {
  21. eventHandler(sender, eventArgs);
  22. }
  23. }
  24. /// <summary>
  25. /// Fires the event if it is not null.
  26. /// </summary>
  27. /// <param name="eventHandler"></param>
  28. /// <param name="sender"></param>
  29. /// <param name="eventArgs"></param>
  30. public static void Fire(this EventHandler eventHandler, object sender, EventArgs eventArgs)
  31. {
  32. if (eventHandler != null)
  33. {
  34. eventHandler(sender, eventArgs);
  35. }
  36. }
  37. /// <summary>
  38. /// Fires the action if not null
  39. /// </summary>
  40. /// <param name="action"></param>
  41. public static void Fire(this Action action)
  42. {
  43. if (action != null)
  44. {
  45. action();
  46. }
  47. }
  48. /// <summary>
  49. /// Fires the action if not null
  50. /// </summary>
  51. /// <param name="action"></param>
  52. /// <param name="t"></param>
  53. public static void Fire<T>(this Action<T> action, T t)
  54. {
  55. if (action != null)
  56. {
  57. action(t);
  58. }
  59. }
  60. /// <summary>
  61. /// Fires the action if not null
  62. /// </summary>
  63. /// <param name="action"></param>
  64. /// <param name="t1"></param>
  65. /// <param name="t2"></param>
  66. public static void Fire<T1, T2>(this Action<T1, T2> action, T1 t1, T2 t2)
  67. {
  68. if (action != null)
  69. {
  70. action(t1, t2);
  71. }
  72. }
  73. /// <summary>
  74. /// Fires the action if not null
  75. /// </summary>
  76. /// <param name="action"></param>
  77. /// <param name="t1"></param>
  78. /// <param name="t2"></param>
  79. /// <param name="t3"></param>
  80. public static void Fire<T1, T2, T3>(this Action<T1, T2, T3> action, T1 t1, T2 t2, T3 t3)
  81. {
  82. if (action != null)
  83. {
  84. action(t1, t2, t3);
  85. }
  86. }
  87. }
  88. }