求救!! --[反射]
现在学到反射, 一头雾水啊!
有人说:
可以copy 对象,
可以找到dll 类中隐藏的方法与属性。
不知怎么理解。
下面是用反射做的copy 对象;---求解
//--猫类public class Cat{ public int Age { get; set; } public string Name { get; set; } //假设它的属性还有很多很多}//--测式Copyprivate 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(),"提示");