zoukankan      html  css  js  c++  java
  • datatable 转 对象

    private static List<T> ConvertToList<T>(DataTable dt)
      where T : new()
      {
       T t1;
       Type type = typeof(T);
       List<T> list = new List<T>();
       PropertyInfo[] propertys = type.GetProperties();
       string name = string.Empty;
       foreach (DataRow dr in dt.Rows)
       {
        T t2 = default(T);
        if (t2 == null)
        {
         t1 = Activator.CreateInstance<T>();
        }
        else
        {
         t2 = default(T);
         t1 = t2;
        }
        T t = t1;
        PropertyInfo[] propertyInfoArray = propertys;
        for (int i = 0; i < (int)propertyInfoArray.Length; i++)
        {
         PropertyInfo pi = propertyInfoArray[i];
         name = pi.Name;
         if (dt.Columns.Contains(name))
         {
          if (!pi.CanWrite)
          {
                                continue;
          }
          object value = SqlHelper.ToCSharpValue(dr[name]);
          if (null != value)
          {
           pi.SetValue(t, value, null);
          }
         }
        
        }
        list.Add(t);
       }
       return list;
      }

  • 相关阅读:
    js参数自定义
    分页插件--记录
    .net mvc接收参数为null的解决方案
    c#枚举转字典或表格
    openlayers添加弹出框
    openlayers按坐标点播放
    openlayers轨迹匀速播放
    MyEclipse配置进行Hibernate逆映射
    BIO,NIO,AIO
    Git遇到的一点错误
  • 原文地址:https://www.cnblogs.com/zhangxiaoshuai/p/6623441.html
Copyright © 2011-2022 走看看