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

问题描述 投票:2回答:2

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

我在这里有这个代码示例:

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循环的索引为函数创建callname。

编辑:

我可能不完全理解如何正确使用EventListeners。为了更好地说明我在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;
}
c# winforms function events call
2个回答
2
投票

导致反思方法的大多数问题都是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);
}

2
投票

这不是反思,而是实现你想要的:

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

(希望我猜对了)

© www.soinside.com 2019 - 2024. All rights reserved.