• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#DataTable转换成ListT

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
运用泛型和反射实现的转换,很给力。代码中掺杂详尽注释,稍微了解一下泛型和反射便可以了解转换的实质。可以直接复制粘贴进行调用哦。

public
 class DtConverToList<T> where T : new() {     public static List<T> DtToList(DataTable dt)     {         //定义集合         List<T> ListCollection = new List<T>(dt.Rows.Count);         //获得 T 模型类型         Type T_type = typeof(T);         //获得 T 模型类型公共属性         PropertyInfo[] Proper = T_type.GetProperties();         //临时变量,存储变量模型公共属性Name         string Tempname = "";         //遍历参数 DataTable的每行         foreach (DataRow Dr in dt.Rows)         {             //实例化 T 模版类             T t = new T();             //遍历T 模版类各个属性             #region             foreach (PropertyInfo P in Proper)             {                 //取出类属性之一                 Tempname = P.Name;                 //判断DataTable中是否有此列                 if (dt.Columns.Contains(Tempname))                 {                     //判断属性是否可写属性                     if (!P.CanWrite)                     {                         continue;                     }                     try                     {                         //得到Datable单元格中的值                         object value = Dr[Tempname];                         //得到 T 属性类型                         Type ProType = P.PropertyType;                         //判断类型赋值                         if (value != DBNull.Value)                         {                             //                             if (value.GetType() == ProType)                             {                                 P.SetValue(t, value, null);                             }                             else                             {                                 if (ProType == typeof(string))                                 {                                     string Temp = value.ToString();                                     P.SetValue(t, Temp, null);                                 }                                 else if (ProType == typeof(byte))                                 {                                     byte Temp = Convert.ToByte(value);                                     P.SetValue(t, Temp, null);                                 }                                 else if (ProType == typeof(short))                                 {                                     short Temp = short.Parse(value.ToString());                                     P.SetValue(t, Temp, null);                                 }                                 else if (ProType == typeof(long))                                 {                                     long Temp = long.Parse(value.ToString());                                     P.SetValue(t, Temp, null);                                 }                                 else if (ProType == typeof(Int64))                                 {                                     Int64 Temp = Convert.ToInt64(value);                                     P.SetValue(t, Temp, null);                                 }                                 else if (ProType == typeof(Int32))                                 {                                     Int32 Temp = Convert.ToInt32(value);                                     P.SetValue(t, Temp, null);                                 }                                 else if (ProType == typeof(Int16))                                 {                                     Int16 Temp = Convert.ToInt16(value);                                     P.SetValue(t, Temp, null);                                 }                                 else                                 {                                     object Temp = Convert.ChangeType(value, ProType);                                     P.SetValue(t, Temp, null);                                 }                             }                         }                     }                     catch (Exception)                     {                         throw;                     }                 }             }             #endregion             ListCollection.Add(t);         }         return ListCollection;     } }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
每天进步一点点C#根据数据画饼图发布时间:2022-07-13
下一篇:
将(Oracle)数据库表导出到Excel,并生成文件(C#实现)发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap