private void Form1_Load(object sender, EventArgs e) { Student stuObj = new Student() { Student_ID = 1, Student_Name = "WD", Student_Sex="N" }; //ceshi GetClassInfo(stuObj); }
/// <summary> /// 根据传入的对象获取信息 /// </summary> private void GetClassInfo(Student stuObj) { Type t = stuObj.GetType(); Type t2 = stuObj.GetType(); PropertyInfo[] pInfo = t.GetProperties(); foreach (var item in pInfo) { string currentName = item.Name; object obj = item.GetValue(stuObj, null); t2.GetProperty(currentName).SetValue(2,obj,null); } }