123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- #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;
- using System.Collections.Generic;
- using System.Text;
- namespace Wayne.Lib.StateEngine
- {
- /// <summary>
- /// 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.
- /// </summary>
- public static class GenericEvent
- {
- #region Static methods
- /// <summary>
- /// 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.
- /// </summary>
- /// <typeparam name="TEventArgs"></typeparam>
- /// <param name="eventType"></param>
- /// <param name="sender"></param>
- /// <param name="eventArgs"></param>
- /// <returns></returns>
- public static GenericEvent<TEventArgs> Create<TEventArgs>(object eventType, object sender, TEventArgs eventArgs) where TEventArgs : EventArgs
- {
- return new GenericEvent<TEventArgs>(eventType, sender, eventArgs);
- }
- #endregion
- }
- /// <summary>
- /// 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.
- /// </summary>
- /// <typeparam name="TEventArgs">The type of the EventArgs. Must be an EventArgs class.</typeparam>
- public class GenericEvent<TEventArgs> : StateEngineEvent where TEventArgs : EventArgs
- {
- #region Fields
- private object sender;
- private TEventArgs eventArgs;
- #endregion
- #region Construction
- /// <summary>
- /// Initializes a new instance of the GenericEvent class.
- /// </summary>
- /// <param name="eventType">Type of the event.</param>
- /// <param name="sender">Sender from the .Net event.</param>
- /// <param name="eventArgs">EventArgs from the .Net event.</param>
- public GenericEvent(object eventType, object sender, TEventArgs eventArgs)
- : base(eventType)
- {
- this.eventArgs = eventArgs;
- this.sender = sender;
- }
- #endregion
- #region Properties
- /// <summary>
- /// EventArgs from the .Net event
- /// </summary>
- public TEventArgs EventArgs
- {
- get { return eventArgs; }
- set { eventArgs = value; }
- }
- /// <summary>
- /// Sender from the .Net event.
- /// </summary>
- public object Sender
- {
- get { return sender; }
- set { sender = value; }
- }
- #endregion
- #region Debug methods
- /// <summary>
- /// Presents the class as a string.
- /// </summary>
- /// <returns></returns>
- 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
- }
- }
|