这可能是一个愚蠢的问题,但我想了解如何更有效地做到这一点。
我在这里有这个代码示例:
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;
}
答案 0 :(得分: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);
}
答案 1 :(得分: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
}
}
(希望我猜对了)