Last active
December 26, 2015 14:49
-
-
Save arxae/7168871 to your computer and use it in GitHub Desktop.
Dispatch tables
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
// Regular dispatch table | |
Dictionary<string, Action> dispatch = new Dictionary<string, Action>(); | |
dispatch["help"] = new Action(() => Console.WriteLine("Hello :3")); | |
dispatch["dosomething"] = new Action(() => | |
{ | |
Console.WriteLine("Do Something else"); | |
}); | |
dispatch["help"](); | |
// Using delegates | |
Dictionary<string, Delegate> dispatch = new Dictionary<string, Delegate>(); | |
dispatch["help"] = new Action(() => Console.WriteLine("Hello")); | |
dispatch["dosomething"] = new Action<string>(s => Console.WriteLine(s)); | |
dispatch["help"].DynamicInvoke(); | |
dispatch["dosomething"].DynamicInvoke("World!"); | |
// Using dynamic (starting from .NET 4) | |
Dictionary<string, dynamic> dispatch = new Dictionary<string, dynamic>(); | |
dispatch["help"] = new Action(() => Console.WriteLine("Hello")); | |
dispatch["dosomething"] = new Action<string>(s => Console.WriteLine(s)); | |
dispatch["help"](); | |
dispatch["dosomething"]("World"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment