小弟求助!!
如何索引一个类中的多个字类啊??
比如:
class A
{
class B:A
{
string Name;
}
class C:A
{
string Name;
}
class D:A
{
string Name;
}
class E:A ..........
}
如上所述
public partial class Form1 : Form
{
int i = 0;
private void button1_Click(object sender, EventArgs e)
{
if(i<3)
{
//想实现点一下button1让,label1.Text = B.Name;
//再点一下button1让,label1.Text = C.Name;
//再点一下button1让,label1.Text = D.Name;
//再点一下button1让,label1.Text = E.Name;
//..........
//再点一下button1让,label1.Text = B.Name;
//再点一下button1让,label1.Text = C.Name;
//........
//如此循环。。。
i++;
}
else
{
i=0;
}
i++;
}
}
[解决办法]
public class Program { public class A : Program { public string Name = "A"; } public class B : Program { public string Name = "B"; } public class C : Program { public string Name = "C"; } public static void Main() { for (int i = 0; i < 3; i++) { Type type = typeof(Program).GetNestedTypes()[i]; Console.WriteLine(type.GetField("Name").GetValue(type.GetConstructors()[0].Invoke(null)).ToString()); } //输出 //A //B //C } }