Skip to content

Instantly share code, notes, and snippets.

@jchadwick
Created April 18, 2012 20:55
Show Gist options
  • Save jchadwick/2416485 to your computer and use it in GitHub Desktop.
Save jchadwick/2416485 to your computer and use it in GitHub Desktop.
Extension Methods to make triggering event handlers easier
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