【问题标题】:Access multiple methods stored in an Action[] dictionary访问存储在 Action[] 字典中的多个方法
【发布时间】:2016-01-19 09:40:40
【问题描述】:

This question covers the use of actions in a dictionary. 我想做类似的事情,但每次操作不止一种方法

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
        };

dotdash 指的是这些私有函数:

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[] 中包含的方法?

【问题讨论】:

  • 对于您的具体情况,我只会将 lamda 存储为(单个)动作。所以... { 'a', () =&gt; { dot(); dash(); }, ...。这使调用保持简单,并且并没有真正改变字典设置。您也可以考虑将字段的类型设为 IReadOnlyDictionary,因为我认为它的内容不会被更改。

标签: c# dictionary console-application morse-code


【解决方案1】:

你快到了。你有一系列动作,所以现在你需要做的就是按顺序执行这些动作。

C# 中的Actions 和Funcs 就像任何其他对象一样 - 您可以将它们放入数组中,将它们分配给变量,将它们作为参数传递给方法,等等。唯一的区别是您可以调用一个动作。它的语法看起来就像调用方法的语法:

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

因此,要执行数组中的操作,您只需 foreach 向下数组将它们一一拉出,然后在循环体中调用每个操作。

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();
        }
    }       
}

【讨论】:

    【解决方案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();
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      使用foreach 迭代一系列动作:

      private static void morseThis(string message){
          char[] messageComponents = message.ToCharArray();
          Action[] currentMorseArray;
          if (morseDictionary.TryGetValue(messageComponents[i], out currentMorseArray))
          {
              foreach(var action in currentMorseArray)
              {
                  action();
              }
          }       
      }
      

      我不确定您对Console.WriteLine 的预期,但您观察到的行为是预期的,因为WriteLine 在对象上调用ToString 以打印它并为数组(和其他类型)其中ToString 未被覆盖)它只返回类型名称。

      【讨论】:

        【解决方案4】:
        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());
                }   
            }    
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-25
          • 2017-06-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-26
          • 1970-01-01
          相关资源
          最近更新 更多