C# GetType()的问题

来源:百度知道 编辑:UC知道 时间:2024/09/28 07:39:09
Class1 c = new Class1();
object ob = (object)c;
Type t = ob.GetType();
PropertyInfo[] b = t.GetProperties();
foreach (PropertyInfo pro in b)
{
this.TextBox1.Text = pro.Name.ToString();
}
Class1列里面:
private int id;

public int Id
{
get { return id; }
set { id = value; }
}
private string name;

public string Name
{
get { return name; }
set { name = value; }
}
private string password;

public string Password
{
get { return password; }
set { password = value; }
}
谁能给我解释一下这段代码,textbox1的Text是多少为什么?

最后值为:Password,你在迭代一个PropertyInfo数组时,这个PropertyInfo 数组已经通过反射获取了当前类型的所有属性,而这些属性是CLR按照你声明的顺序去将C#编译为MSIL,Password排列在最后,迭代到最后,自然就剩下Password属性名了。

反过来想txt是string类型
获取当前实例的 System.Type。
返回结果:
System.Type 实例,表示当前实例的确切运行时类型。