所以,我想要做的是迭代x值的数组,并且对于每次迭代,获取标签“plabel”+ x的当前文本值(标签都已经创建并命名。我是相对的使用反射的新手,但从我读过的内容应该有效:
PropertyInfo pI;
pI = this.GetType().GetProperty("plabel" + count + ".Text"); //count is the current iteration #
MessageBox.Show(pI.Name);
但这会引发运行时异常。有人可以告诉我这样做的正确方法吗?
答案 0 :(得分:2)
尝试这样的事情:
//Gets the label (includes private fields)
FieldInfo fi = this.GetType().GetField("plabel" + count,
BindingFlags.NonPublic | BindingFlags.Instance);
Label label = fi.GetValue(this) as Label;
if (label != null)
{
MessageBox.Show(label.Text);
}