#region --------------- Copyright Dresser Wayne Pignone ------------- /* * $Log: /Wrk/WayneLibraries/Wrk/StateEngine/GenericEvent.cs $ * * 5 07-03-12 15:17 roger.månsson * Documentation update. * * 4 07-03-02 13:18 roger.månsson * * 3 07-03-02 13:17 roger.månsson * * 2 07-03-02 12:54 roger.mnsson * Added static GenericEvent class that is used to create GenericEvent * without needing to specify the type argument in C#. */ #endregion using System; namespace Wayne.Lib.StateEngine { /// /// Static class that can be used only as a substitute for the GenericEvent<> constructor. /// It allows C# developers to avoid typing the type argument by inferred parameter type resolution. /// public static class GenericEvent { #region Static methods /// /// Static method that can be used instead of the constructor. The advantage is /// that by using a method C# can figure out the type parameter by the input /// argument, and thus decreasing the amount of code to write. /// /// /// /// /// /// public static GenericEvent Create(object eventType, object sender, TEventArgs eventArgs) where TEventArgs : EventArgs { return new GenericEvent(eventType, sender, eventArgs); } #endregion } /// /// GenericEvent is an event that can be used with .Net Events and delegates. Instead of defining each /// event as both an EventArgs structure and an StateEngineEvent structure with about the same content, /// this class is a StateEngineEvent that can contain information from a .Net event. /// /// The type of the EventArgs. Must be an EventArgs class. public class GenericEvent : StateEngineEvent where TEventArgs : EventArgs { #region Fields private object sender; private TEventArgs eventArgs; #endregion #region Construction /// /// Initializes a new instance of the GenericEvent class. /// /// Type of the event. /// Sender from the .Net event. /// EventArgs from the .Net event. public GenericEvent(object eventType, object sender, TEventArgs eventArgs) : base(eventType) { this.eventArgs = eventArgs; this.sender = sender; } #endregion #region Properties /// /// EventArgs from the .Net event /// public TEventArgs EventArgs { get { return eventArgs; } set { eventArgs = value; } } /// /// Sender from the .Net event. /// public object Sender { get { return sender; } set { sender = value; } } #endregion #region Debug methods /// /// Presents the class as a string. /// /// public override string ToString(string format, IFormatProvider provider) { return String.Format(provider, "{0}, EventArgs: {1}", base.ToString(format, provider), ((eventArgs != null) ? eventArgs.ToString() : " null")); } #endregion } }