This question covers the use of actions in a dictionary. I want to do something similar, but with more than one method per action:
static readonly Dictionary<char, Action[]> morseDictionary = new Dictionary<char,Action[]>
{
{ 'a', new Action[] {dot, dash} },
{ 'b', new Action[] {dash, dot, dot, dot} },
{ 'c', new Action[] {dash, dot, dash, dot} },
{ 'd', new Action[] {dash, dot, dot} },
{ 'e', new Action[] {dot} }
// etc
};
dot
and dash
refer to these private functions:
private static void dash(){
Console.Beep(300, timeUnit*3);
}
private static void dot(){
Console.Beep(300, timeUnit);
}
I have another function, morseThis
, which is designed to convert a message string to audio output:
private static void morseThis(string message){
char[] messageComponents = message.ToCharArray();
if (morseDictionary.ContainsKey(messageComponents[i])){
Action[] currentMorseArray = morseDictionary[messageComponents[i]];
Console.WriteLine(currentMorseArray); // prints "System.Action[]"
}
}
In the example above, I am able to print "System.Action[]" to the console for every letter contained in the input message. However, my intention is to call the methods within currentMorseArray
in order.
How do I access the methods contained within a given Action[] in my dictionary?