1.封装的方法
public static T GetT<T>(T t, List<string> list) { System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(); for (int i = 0; i < properties.Length; i++) { properties[i].SetValue(t, list[i]); } return t; }
2.Model类
public class Staff { public string id { get; set; } public string barcode { get; set; } public string name { get; set; } }
3.使用实列
private void button1_Click(object sender, EventArgs e) { Staff staff = new Staff(); ; List<string> list = new List<string>() { "1","2","3"}; for (int i = 0; i < 3; i++) { list.Add((i+1).ToString()); } staff = GetT<Staff>(staff, list); }
这样看一下staff中已经有值了。