GenericEvent.cs 3.7 KB

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