动态地通过名称获取变量 - 没有反射

时间:2014-04-10 21:53:58

标签: c# arrays c#-4.0

我目前正在逐一做这件事:整理他们

             //created on the form1    
textboxs[0] = textbox0;  
textboxs[1] = textbox1; 
textboxs[2] = textbox2;  
textboxs[3] = textbox3;

有办法以某种方式将它转换成下面的吗?

for (int i = 0; i < KSy; i++)  //kisi sayısı
{
    // getting the i as code (not variable)
    textboxs[i] = textbox+i ;
}

更新 这就是我想要做的: Find control by name from Windows Forms controls

每个人都在谈论反思。我努力使反思工作

1 个答案:

答案 0 :(得分:0)

假设cb_kisiN是您可以执行的字段:

for(int i =  0; i < KSy; i++)
{
    var field = this.GetType().GetField("textbox" + i);
    var code = field.GetValue(this) as Code;
    textboxs[i] = code;
}