Skip to content

Instantly share code, notes, and snippets.

@gsscoder
Last active January 21, 2020 13:44
Show Gist options
  • Save gsscoder/c287bad9bb98fc950fbf3c4b466c1dbc to your computer and use it in GitHub Desktop.
Save gsscoder/c287bad9bb98fc950fbf3c4b466c1dbc to your computer and use it in GitHub Desktop.
C# helper class for raising events
using System;
using System.Runtime.CompilerServices;
static class EventHelper
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RaiseEvent(object sender, EventHandler handler, EventArgs args, bool enabled)
{
if (enabled && handler != null) {
handler(sender, args);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RaiseEvent<T>(object sender, EventHandler<T> handler, Func<T> args, bool enabled)
where T : EventArgs
{
if (enabled && handler != null) {
handler(sender, args());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment