1

この質問は、辞書でのアクションの使用をカバーしています。同様のことをしたいのですが、アクションごとに複数のメソッドを使用します。

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[] に含まれるメソッドにアクセスするにはどうすればよいですか?

4

4 に答える 4

2

もうすぐそこです。アクションの配列があるので、あとはアクションを順番に実行するだけです。

ActionC# のs とFuncs は、他のオブジェクトと同じように、配列に入れたり、変数に代入したり、引数としてメソッドに渡したりすることができます。唯一の違いは、アクションを呼び出せることです。この構文は、メソッドを呼び出すための構文と同じように見えます。

Action myAction = ...
myAction();  // call the action

したがって、配列内のアクションを実行するにはforeach、配列を下って 1 つずつ引き出し、ループの本体でそれぞれを呼び出します。

private static void morseThis(string message)
{
    char[] messageComponents = message.ToCharArray();

    if (morseDictionary.ContainsKey(messageComponents[i]))
    {
        Action[] currentMorseArray = morseDictionary[messageComponents[i]];

        foreach (Action action in currentMorseArray)
        {
            action();
        }
    }       
}
于 2015-10-20T16:15:57.057 に答える
2

アクションを列挙して実行します。

private static void morseThis(string message)
{
    char[] messageComponents = message.ToCharArray();

    foreach(char c in messageComponents)
    {
        Action[] actions;

        if (morseDictionary.TryGetValue(c, out actions))
        {
            foreach(Action action in actions)
            {
              action();
            }
        }
    }
}
于 2015-10-20T16:17:21.123 に答える
0
static readonly Dictionary<char, List<Action>> morseDictionary = new Dictionary<char,List<Action>>
        {
            { 'a', new List<Action> {dot, dash} },
            { 'b', new List<Action> {dash, dot, dot, dot} },
            { 'c', new List<Action> {dash, dot, dash, dot} },
            { 'd', new List<Action> {dash, dot, dot} },
            { 'e', new List<Action> {dot} }
            // etc
        };

private static void morseThis(string messageSrc)
{
    foreach(char message in messageSrc.ToCharArray())
    {
        List<Action> currentMorseArray;
        if (morseDictionary.TryGetValue(message, out currentMorseArray))
        {
            currentMorseArray.ForEach(x=>x());
        }   
    }    
}
于 2015-10-20T16:25:13.153 に答える