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>());
}

  

  

相关文章:

  • 2021-08-15
  • 2021-10-16
  • 2022-12-23
  • 2022-01-04
  • 2021-09-24
  • 2022-12-23
  • 2021-11-01
  • 2021-06-15
猜你喜欢
  • 2021-06-11
  • 2021-09-11
  • 2021-10-18
  • 2021-05-23
  • 2022-12-23
  • 2021-08-18
  • 2021-09-21
相关资源
相似解决方案