Created
April 18, 2012 20:55
-
-
Save jchadwick/2416485 to your computer and use it in GitHub Desktop.
Extension Methods to make triggering event handlers easier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public static class EventHandlerExtensions | |
{ | |
public static void SafeInvoke(this EventHandler handler, EventArgs args = null, object sender = null) | |
{ | |
if (handler != null) | |
handler(sender, args ?? EventArgs.Empty); | |
} | |
public static void SafeInvoke<TData>(this EventHandler<EventArgs<TData>> handler, TData data, object sender = null) | |
{ | |
if (handler != null) | |
handler(sender, new EventArgs<TData>(data)); | |
} | |
public static void SafeInvoke<TEventArgs>(this EventHandler<TEventArgs> handler, TEventArgs args = null, object sender = null) | |
where TEventArgs : EventArgs | |
{ | |
if (handler != null) | |
handler(sender, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment