1. /// <summary>
  2. /// 实体转换辅助类
  3. /// </summary>
  4. public class ModelConvertHelper<T> where T : new()
  5. {
  6. public static IList<T> ConvertToModelList(DataTable dt)
  7. {
  8. // 定义集合
  9. IList<T> ts = new List<T>();
  10. // 获得此模型的类型
  11. Type type = typeof(T);
  12. string tempName = "";
  13. foreach (DataRow dr in dt.Rows)
  14. {
  15. T t = new T();
  16.  
  17. // 获得此模型的公共属性
  18. PropertyInfo[] propertys = t.GetType().GetProperties();
  19. foreach (PropertyInfo pi in propertys)
  20. {
  21. tempName = pi.Name;
  22. // 检查DataTable是否包含此列
  23. if (dt.Columns.Contains(tempName))
  24. {
  25. // 判断此属性是否有Setter
  26. if (!pi.CanWrite) continue;
  27. object value = dr[tempName];
  28. if (value != DBNull.Value)
  29. pi.SetValue(t, value, null);
  30. }
  31. }
  32. ts.Add(t);
  33. }
  34. return ts;
  35. }

  

.NET 实体转换辅助类的更多相关文章

  1. DataTable转List<Model>通用类【实体转换辅助类】

    /// <summary> /// DataTable转List<Model>通用类[实体转换辅助类] /// </summary> public class Mo ...

  2. Datatable转实体 实体转换辅助类

    using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R ...

  3. HBaseConvetorUtil 实体转换工具

    HBaseConvetorUtil 实体转换工具类 public class HBaseConvetorUtil {        /**    * @Title: convetor    * @De ...

  4. html实体转换

    摘要: 在 HTML 中,某些字符是预留的.在 HTML 中不能使用小于号(<)和大于号(>),这是因为浏览器会误认为它们是标签.如果希望正确地显示预留字符,我们必须在 HTML 源代码中 ...

  5. C# 获取config文件 实体转换

    随着项目的扩展,单独的key,value配置文件已经不能满足需求了 这里需要自定义配置节点,例如 <!--自定义 具体实体类配置问节点信息--> <School Name=" ...

  6. C# 实体集合和实体转换成相应的string、XDocument、XElement、XDocument

    https://msdn.microsoft.com/zh-cn/library/system.xml.linq.xelement(v=vs.110).aspx XElement.Parse 方法 ( ...

  7. C#中实体集合和实体转换成相应的string、XDocument、XElement

    C#中实体集合和实体转换成相应的string.XDocument.XElement public class SimpleXmlConverter { public static string ToX ...

  8. .Net Core2.2 使用 AutoMapper进行实体转换

    一.遇到的问题 在. Core Api 的编写中,我们经常会对一些功能点进行新增编辑操作,同时我们有时也会进行查询,但是我们查询的表的数据与我们返回的数据相差甚大,这是我们有需要自己手动进行类型的转换 ...

  9. mapstruct 实体转换及List转换,@Mapper注解转换

    本文参考 https://blog.csdn.net/u012373815/article/details/88367456 主要是为了自己使用方便查询. 这些都是我平时用到了,大家有什么好方法或者有 ...

随机推荐

  1. iptables的启动和关闭【转载】

    原文网址:http://os.51cto.com/art/201103/249049.htm iptables的启动和关闭: 1.启动和关闭iptables 下面将正式使用iptables来创建防火墙 ...

  2. java编程思想第五章初始化与清理

    5.1使用构造器确保初始化: 构造器与一般方法一样,但是没有返回值,且其方法名与类名完全相同. 不接受任何参数的构造器成为默认构造器,也叫无参构造器. 5.2 方法重载: 为什么会有方法重载? 构造器 ...

  3. linux环境下搭建jenkins实现自动部署

    写在前面:公司项目初期,环境一切从始.因此,项目的发布环境需要自己搭建.就动手搭建了jenkins,在此把个人的搭建过程以及搭建中碰到的问题一起总结一下. 1. 准备环境. 首先,需要jdk是必须要安 ...

  4. PCIE编程1:lspci操作

    lspci 是一个用来显示系统中所有PCI总线设备或连接到该总线上的所有设备的工具. 列出所有的PCIE设备: lspci 选项: -v 使得 lspci 以冗余模式显示所有设备的详细信息. -vv ...

  5. LdapContext获取对象的属性

    //            dn = "cn=1,cn=Users,DC=域名,DC=COM";//            Attributes answer = ctx.getA ...

  6. Annotation之四:注解中的-Xlint:unchecked和 -Xlint:deprecation

    一.-Xlint:unchecked用法 对如下Test.java编译时 package com.dxz.annotation; import java.util.ArrayList; import ...

  7. Oracle RMAN 学习

    Oracle RMAN 学习:三思笔记 1 进入rman Rman--物理备份(结构/数据) 1 本地db Cmd set oracle_sid=orcl 1 rman target / Rman&g ...

  8. linux命令 把Windows 文件拷贝到linux

    scp build.zip mesadmin@dpydalapp01.sl.bluecloud.ibm.com:/tmp   // 把Windows上的build.zip拷贝到mesadmin@dpy ...

  9. rails自定义出错页面

    一.出错类型 Exception ActionController::UnknownController, ActiveRecord::RecordNotFound ActionController: ...

  10. angular之增删改查

    <!DOCTYPE html> <html lang="en" ng-app="app"> <head> <meta ...