この質問は、辞書でのアクションの使用をカバーしています。同様のことをしたいのですが、アクションごとに複数のメソッドを使用します。
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
これらのプライベート関数をdash
参照してください。
private static void dash(){
Console.Beep(300, timeUnit*3);
}
private static void dot(){
Console.Beep(300, timeUnit);
}
morseThis
メッセージ文字列をオーディオ出力に変換するように設計された別の関数 があります。
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[]"
}
}
上記の例では、入力メッセージに含まれるすべての文字に対して「System.Action[]」をコンソールに出力できます。ただし、私の意図は、メソッドcurrentMorseArray
を順番に呼び出すことです。
辞書内の特定の Action[] に含まれるメソッドにアクセスするにはどうすればよいですか?