Skip to content

Instantly share code, notes, and snippets.

@bittercoder
Created November 5, 2012 00:58
Show Gist options
  • Save bittercoder/4014624 to your computer and use it in GitHub Desktop.
Save bittercoder/4014624 to your computer and use it in GitHub Desktop.
Generic list of actions
public class GenericActionList
{
readonly IList<Action<object>> _actions = new List<Action<object>>();
public void AddAction<T>(Action<T> action)
where T : class
{
_actions.Add(input => Invoke(action, input));
}
void Invoke<T>(Action<T> action, object input)
where T : class
{
try
{
T inputAsT = input as T;
if (inputAsT != null)
{
action(inputAsT);
}
}
catch (Exception ex)
{
throw new Exception(string.Format("Failed to execute action of type: {0} using input of type: {1}", typeof(T), input.GetType()), ex);
}
}
public void InvokeAll(object input)
{
foreach (var action in _actions) action(input);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment