zoukankan      html  css  js  c++  java
  • ModelConvertHelper(将DataTable转换成List<model>)

    public class ModelConvertHelper<T> where T : new()
    {
         public static IList<T> ConvertToModel(DataTable dt)
         {
             // Collection definition
             IList<T> ts = new List<T>();

             // Get model type
             Type type = typeof(T);

             string tempName = "";

             foreach (DataRow dr in dt.Rows)
             {
                 T t = new T();

                 // Get property of model
                 PropertyInfo[] propertys = t.GetType().GetProperties();

                 foreach (PropertyInfo pi in propertys)
                 {
                     tempName = pi.Name;

                     // check column is exsit
                     if (dt.Columns.Contains(tempName))
                     {
                         // Whether can be set value
                         if (!pi.CanWrite) continue;

                         object value = dr[tempName];
                         if (value != DBNull.Value)
                             pi.SetValue(t, value, null);
                     }
                 }

                 ts.Add(t);
             }

             return ts;
         }
    }

  • 相关阅读:
    程序中图片透明 函数(使用SetBkColor API函数)
    编程中使用锁
    event内存泄漏
    diskcache
    linux内核管理
    Vue
    Paxos算法
    索引以及页和区
    CoreRT
    二叉树
  • 原文地址:https://www.cnblogs.com/key1309/p/3566567.html
Copyright © 2011-2022 走看看