123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using System;
- namespace Wayne.Lib
- {
- /// <summary>
- /// Contains extension methods for firing events.
- /// </summary>
- public static class Events
- {
- /// <summary>
- /// Fires the event if it is not null.
- /// </summary>
- /// <typeparam name="TEventArgs"></typeparam>
- /// <param name="eventHandler"></param>
- /// <param name="sender"></param>
- /// <param name="eventArgs"></param>
- public static void Fire<TEventArgs>(this EventHandler<TEventArgs> eventHandler, object sender, TEventArgs eventArgs)
- where TEventArgs : EventArgs
- {
- var threadSafeEventHandler = eventHandler;
- if (threadSafeEventHandler != null)
- {
- threadSafeEventHandler(sender, eventArgs);
- }
- }
- /// <summary>
- /// Fires the event if it is not null.
- /// </summary>
- /// <param name="eventHandler"></param>
- /// <param name="sender"></param>
- /// <param name="eventArgs"></param>
- public static void Fire(this EventHandler eventHandler, object sender, EventArgs eventArgs)
- {
- var threadSafeEventHandler = eventHandler;
- if (threadSafeEventHandler != null)
- {
- threadSafeEventHandler(sender, eventArgs);
- }
- }
- /// <summary>
- /// Fires the action if not null
- /// </summary>
- /// <param name="action"></param>
- public static void Fire(this Action action)
- {
- if (action != null)
- {
- action();
- }
- }
- /// <summary>
- /// Fires the action if not null
- /// </summary>
- /// <param name="action"></param>
- /// <param name="t"></param>
- public static void Fire<T>(this Action<T> action, T t)
- {
- if (action != null)
- {
- action(t);
- }
- }
- /// <summary>
- /// Fires the action if not null
- /// </summary>
- /// <param name="action"></param>
- /// <param name="t1"></param>
- /// <param name="t2"></param>
- public static void Fire<T1, T2>(this Action<T1, T2> action, T1 t1, T2 t2)
- {
- if (action != null)
- {
- action(t1, t2);
- }
- }
- /// <summary>
- /// Fires the action if not null
- /// </summary>
- /// <param name="action"></param>
- /// <param name="t1"></param>
- /// <param name="t2"></param>
- /// <param name="t3"></param>
- public static void Fire<T1, T2, T3>(this Action<T1, T2, T3> action, T1 t1, T2 t2, T3 t3)
- {
- if (action != null)
- {
- action(t1, t2, t3);
- }
- }
- }
- }
|