public static T ConvertToModel<T>(this DataRow dr) { T t = Activator.CreateInstance<T>(); //创建实例 PropertyInfo[] pi = t.GetType().GetProperties();//取类的属性 //属性赋值 foreach (PropertyInfo p in pi) { if (dr.Table.Columns.Contains(p.Name) && !string.IsNullOrWhiteSpace(dr[p.Name].ToString())) { p.SetValue(t, Convert.ChangeType(dr[p.Name], p.PropertyType), null); } } return t; //Return }
List<CommPay> list = new List<CommPay>(); foreach (DataRow dr in dt.Rows) { list.Add(dr.ConvertToModel<CommPay>()); }
时间: 2024-10-31 14:36:50