110,534
社区成员
发帖
与我相关
我的任务
分享
//--猫类
public class Cat
{
public int Age { get; set; }
public string Name { get; set; }
//假设它的属性还有很多很多
}
//--测式Copy
private void button3_Click(object sender, EventArgs e)
{
Cat c1 = new Cat() { Age = 20, Name = "小猫" };
Type type = typeof(Cat);
Cat c2 = new Cat();
//---下面这段的foreach 是什么意思?它做了什么?
foreach (PropertyInfo info in type.GetProperties())
{
info.SetValue(c2, info.GetValue(c1, null), null);
}
labName.Text = c2.Name;
labAge.Text = c2.Age.ToString();
}
Cat c1 = new Cat() { Age = 20, Name = "小猫" };
Type type = typeof(Cat);
StringBuilder sb = new StringBuilder();
foreach (PropertyInfo info in type.GetProperties())
{
sb.Append(info.Name+"\t"+info.GetValue(c1, null).ToString()+"\r\n");
}
MessageBox.Show(sb.ToString(),"提示");