创建并 运行 动作字典

Create and run dictionary of actions

我正在尝试创建一个动作字典,然后 运行 它们循环。 This 问题有帮助,但在将操作添加到字典时我仍然遇到编译错误:- No overload for 'Action' matches delegate 'Action'.

感谢您的帮助。

Dictionary<string, Action> dActions = new Dictionary<string, Action>();
            // do the actions need to be created?
            Action<string, int> actSpot = new Action<string, int>(oBSM.Spot);
            Action<string, int> actDelta = new Action<string, int>(oBSM.Delta);
            dActions["Spot"] = new Action(actSpot);
            dActions["Delta"] = new Action(actDelta);

            // or add action to dictionary?
            dActions.Add("Spot", oBSM.Spot(string BookOrComp, int DP);
            dActions.Add("Delta", oBSM.Delta(string BookOrComp, int DP);

            foreach (var BookOrComp in ListBookOrComp)
            {
                foreach (string Key in dActions.Keys)
                {
                    for (int DP = 1; DP <= 21; DP++)
                    {
                        dActions[Key]();
                    }
                }
            }

更新: 如代码所示,我仍然遇到一些编译错误

Dictionary<string, Action> dActions = new Dictionary<string, Action>();
            // create actions
            Action<string, int> actSpot = new Action<string, int>(oBSM.Spot);  
            Action<string, int> actDelta = new Action<string, int>(oBSM.Delta);  
            dActions["Spot"] = new Action(actSpot); // no overload for Action matches delegate Action
            dActions["Delta"] = new Action(actDelta); // ditto


            foreach (var BookOrComp in ListBookOrComp)
            {
                foreach (string Key in dActions.Keys)
                {
                    for (int DP = 1; DP <= 21; DP++)
                    {
                        dActions[Key](BookOrComp,DP);  // delegate Action  does not take 2 arguments
                    }
                }
            }

我在你的程序中看到了一些错误:

1- 括号不平衡:

        // or add action to dictionary?
        dActions.Add("Spot", oBSM.Spot(string BookOrComp, int DP);
        dActions.Add("Delta", oBSM.Delta(string BookOrComp, int DP);

您需要在此处添加右括号。此外,我不确定该语法是否正确,我会创建一个对象,然后将其添加到字典中。

2- 该动作有两个参数,一个字符串和一个整数:

        Action<string, int> actSpot = new Action<string, int>(oBSM.Spot);
        Action<string, int> actDelta = new Action<string, int>(oBSM.Delta);

但是你调用它的时候根本就没有参数:

         foreach (var BookOrComp in ListBookOrComp)
        {
            foreach (string Key in dActions.Keys)
            {
                for (int DP = 1; DP <= 21; DP++)
                {
                    dActions[Key](); // <<<-- where are the parameters?
                }
            }
        }

我认为这是编译器抱怨的错误。

更新二:

Dictionary<string, Action> dActions = new Dictionary<string, Action>();

应定义为:

Dictionary<string, Action<string, int>> dActions 

=新词典>();

        dActions["Spot"] = new Action(actSpot); // no overload for Action matches delegate Action

应该是

        dActions["Spot"] = actSpot; // actSpot already created with new Action...

或:

        dActions["Spot"] = new Action<string, int>(oBSM.Spot);

PS:

你必须明白,当你这样做的时候:

        dActions["Delta"] = new Action(actDelta); // ditto

您正在使用 Action<string. int> 类型的参数调用 Action 的构造函数,而 Action 没有该构造函数。