Created
November 5, 2012 00:58
-
-
Save bittercoder/4014624 to your computer and use it in GitHub Desktop.
Generic list of actions
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
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