使用 for 循环的实例编号作为函数调用名称的一部分

Using the instance number of a for loop as part of the call name for a function

这可能是一个愚蠢的问题,但我想了解如何更有效地做到这一点。

我这里有这个代码示例:

static void Main(string[] args)
{
    string callName = "someFunction";
    for(int i = 0; i<2;i++)
    {
        callName = callName + Convert.ToString(i);
        //call callName function
    }

    Console.ReadLine();

}

public static void someFunction1()
{
    Console.WriteLine("1");
}
public static void someFunction2()
{
    Console.WriteLine("2");
}

我想根据 for 循环的索引为函数创建调用名。

编辑:

我可能不完全理解如何正确使用 EventListeners。为了更好地说明我在使用 windows 表单时遇到的问题,我正在制作一个 windows 表单,其中在 Panel[] 中有一堆面板。我想使用我的循环索引将动作侦听器添加到每个单独的面板。

public test()
{
    Panel[,] board = new Panel[2, 2];
    bool colorChange = true;
    //Create checkered panels
    for (int column = 0; column < 2; column++)
    {
        for (int row = 0; row < 2; row++)
        {
            if (colorChange)
            {
                Panel currentPanel = CreatePanel(column * 100, row * 100 + 25, 100, 100, 255, 255, 255);
                board[column, row] = currentPanel;
                colorChange = false;
            }
            else
            {
                Panel currentPanel = CreatePanel(column * 100, row * 100 + 25, 100, 100, 0, 0, 0);
                board[column, row] = currentPanel;
                colorChange = true;
            }
        }
        colorChange = !colorChange;
    }
    //unpack panels add controls to the form
    for (int column = 0; column < 2; column++)
    {
        for (int row = 0; row < 2; row++)
        {
            this.Controls.Add(board[column, row]);
        }
    }
    //Could for loop here... But what about event?
    board[0, 0].Click += new EventHandler(asd); //??
}

private void asd(object sender, System.EventArgs e)
{
    MessageBox.Show("hi");
}
//Is there a better way to handle this part??
private void asd2(object sender, System.EventArgs e)
{
    MessageBox.Show("hi2");
}

public static Panel CreatePanel(int x, int y, int width, int height, int r, int g, int b)
{
    Panel myPanel = new Panel();
    myPanel.Location = new Point(x, y);
    myPanel.Size = new Size(width, height);
    int[] rgb = { r, g, b };
    myPanel.BackColor = Color.FromArgb(rgb[0], rgb[1], rgb[2]);
    myPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    myPanel.Show();
    return myPanel;
}

导致反思方法的大多数问题是 XY-problems。你要 X 但你实际上想要 Y 并且认为你需要 X。我确定这是一个。

为什么不简单地实现带有参数的单个方法?

public static void PrintNumber(int number)
{
    Console.WriteLine(number);
}

然后你可以在你的循环中调用它:

for(int i = 0; i < 2; i++)
{
    PrintNumber(i);
}

如果你真的需要反射方法:

Type t = typeof(Program);
var methods = t.GetMethods(BindingFlags.Public | BindingFlags.Static)
    .Where(m => m.Name.StartsWith("someFunction"))
    .ToArray();

for (int i = 1; i <= 2; i++)
{
    MethodInfo method = methods.Single(m => m.Name == "someFunction" + i);
    method.Invoke(null, null);
}

这不会是反射,会实现你想要的:

class Program
{
    public static void someFunction1()
    {
        Console.WriteLine("1");
    }

    public static void someFunction2()
    {
        Console.WriteLine("2");
    }

    static void Main(string[] args)
    {
        var functionList = new System.Collections.Generic.Dictionary<string, System.Delegate>() {
            { nameof(someFunction1), new Action(() => someFunction1()) },
            { nameof(someFunction2), new Action(() => someFunction2()) },
        };

        string callNameBase = "someFunction";
        for (int i = 1; i <= 2; i++)
        {
            string callName = callNameBase + i.ToString();

            functionList[callName].DynamicInvoke();
        }

        Console.ReadLine();
    }
}

(注意:我修复了您示例中错误的索引:1、2 与 0、1 以及您构建函数名称的方式)


但是,如前所述,可能会有更好的做法来实现您想要的。

也许是某种策略模式。

但由于我们不知道你到底想达到什么目的,所以很难给你更好的建议。


编辑 在您提出详细问题后:

显然,您需要一个处理程序用于一半的面板,另一个处理程序用于另一半。然后您可以在创建过程中附加正确的事件处理程序:

    for (int row = 0; row < 2; row++)
    {
        if (colorChange)
        {
            Panel currentPanel = CreatePanel(column * 100, row * 100 + 25, 100, 100, 255, 255, 255);
            board[column, row] = currentPanel;
            colorChange = false;
            board[column, row].Click += new EventHandler(asd); // here
        }
        else
        {
            Panel currentPanel = CreatePanel(column * 100, row * 100 + 25, 100, 100, 0, 0, 0);
            board[column, row] = currentPanel;
            colorChange = true;
            board[column, row].Click += new EventHandler(asd2); // and here
        }
    }

(希望我没猜错)