GenericEvent.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #region --------------- Copyright Dresser Wayne Pignone -------------
  2. /*
  3. * $Log: /Wrk/WayneLibraries/Wrk/StateEngine/GenericEvent.cs $
  4. *
  5. * 5 07-03-12 15:17 roger.månsson
  6. * Documentation update.
  7. *
  8. * 4 07-03-02 13:18 roger.månsson
  9. *
  10. * 3 07-03-02 13:17 roger.månsson
  11. *
  12. * 2 07-03-02 12:54 roger.mnsson
  13. * Added static GenericEvent class that is used to create GenericEvent
  14. * without needing to specify the type argument in C#.
  15. */
  16. #endregion
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Text;
  20. namespace Wayne.Lib.StateEngine
  21. {
  22. /// <summary>
  23. /// Static class that can be used only as a substitute for the GenericEvent&lt;&gt; constructor.
  24. /// It allows C# developers to avoid typing the type argument by inferred parameter type resolution.
  25. /// </summary>
  26. public static class GenericEvent
  27. {
  28. #region Static methods
  29. /// <summary>
  30. /// Static method that can be used instead of the constructor. The advantage is
  31. /// that by using a method C# can figure out the type parameter by the input
  32. /// argument, and thus decreasing the amount of code to write.
  33. /// </summary>
  34. /// <typeparam name="TEventArgs"></typeparam>
  35. /// <param name="eventType"></param>
  36. /// <param name="sender"></param>
  37. /// <param name="eventArgs"></param>
  38. /// <returns></returns>
  39. public static GenericEvent<TEventArgs> Create<TEventArgs>(object eventType, object sender, TEventArgs eventArgs) where TEventArgs : EventArgs
  40. {
  41. return new GenericEvent<TEventArgs>(eventType, sender, eventArgs);
  42. }
  43. #endregion
  44. }
  45. /// <summary>
  46. /// GenericEvent is an event that can be used with .Net Events and delegates. Instead of defining each
  47. /// event as both an EventArgs structure and an StateEngineEvent structure with about the same content,
  48. /// this class is a StateEngineEvent that can contain information from a .Net event.
  49. /// </summary>
  50. /// <typeparam name="TEventArgs">The type of the EventArgs. Must be an EventArgs class.</typeparam>
  51. public class GenericEvent<TEventArgs> : StateEngineEvent where TEventArgs : EventArgs
  52. {
  53. #region Fields
  54. private object sender;
  55. private TEventArgs eventArgs;
  56. #endregion
  57. #region Construction
  58. /// <summary>
  59. /// Initializes a new instance of the GenericEvent class.
  60. /// </summary>
  61. /// <param name="eventType">Type of the event.</param>
  62. /// <param name="sender">Sender from the .Net event.</param>
  63. /// <param name="eventArgs">EventArgs from the .Net event.</param>
  64. public GenericEvent(object eventType, object sender, TEventArgs eventArgs)
  65. : base(eventType)
  66. {
  67. this.eventArgs = eventArgs;
  68. this.sender = sender;
  69. }
  70. #endregion
  71. #region Properties
  72. /// <summary>
  73. /// EventArgs from the .Net event
  74. /// </summary>
  75. public TEventArgs EventArgs
  76. {
  77. get { return eventArgs; }
  78. set { eventArgs = value; }
  79. }
  80. /// <summary>
  81. /// Sender from the .Net event.
  82. /// </summary>
  83. public object Sender
  84. {
  85. get { return sender; }
  86. set { sender = value; }
  87. }
  88. #endregion
  89. #region Debug methods
  90. /// <summary>
  91. /// Presents the class as a string.
  92. /// </summary>
  93. /// <returns></returns>
  94. public override string ToString(string format, IFormatProvider provider)
  95. {
  96. return String.Format(provider, "{0}, EventArgs: {1}",
  97. base.ToString(format, provider),
  98. ((eventArgs != null) ? eventArgs.ToString() : " null"));
  99. }
  100. #endregion
  101. }
  102. }