码迷,mamicode.com
首页 > 其他好文 > 详细

datatable 转 对象

时间:2017-03-26 18:04:07      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:har   can   setvalue   ++   rtt   arp   对象   sha   str   

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

datatable 转 对象

标签:har   can   setvalue   ++   rtt   arp   对象   sha   str   

原文地址:http://www.cnblogs.com/zhangxiaoshuai/p/6623441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!