Skip to content

Instantly share code, notes, and snippets.

@trcio
Last active August 29, 2015 14:02
Show Gist options
  • Save trcio/c34c7d609911b366d81f to your computer and use it in GitHub Desktop.
Save trcio/c34c7d609911b366d81f to your computer and use it in GitHub Desktop.
Callbackr: Easy callback class, for all your callback needs!
public static class Callbackr
{
private static Dictionary<object, Action> callbacks = new Dictionary<object, Action>();
/// <summary>
/// Adds the specified callback with the specified identifier.
/// </summary>
/// <param name="token">The callbacks specified identifier.</param>
/// <param name="callback">The callback to be added.</param>
public static void Add(object token, Action callback)
{
if (callbacks.ContainsKey(token))
throw new InvalidOperationException("Token already exists.");
callbacks.Add(token, callback);
}
/// <summary>
/// Removes the specified callback.
/// </summary>
/// <param name="token">The callbacks specified identifier.</param>
public static void Remove(object token)
{
if (!callbacks.ContainsKey(token))
throw new InvalidOperationException("Token does not exist.");
callbacks.Remove(token);
}
/// <summary>
/// Executes the specified callback.
/// </summary>
/// <param name="token">The callbacks specified identifier.</param>
public static bool Execute(object token)
{
if (callbacks.ContainsKey(token))
{
callbacks[token]();
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment