錶轉List泛型數組

來源:互聯網
上載者:User

標籤:datatable   public   數組   system   class   rem   typedef   setvalue   efi   

轉換那塊怕忘記,留存一下

using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Reflection;using System.Text;namespace App.Util{    public class DtConvertListHelper<T> where T : new()    {        /// <summary>          /// 利用反射和泛型          /// </summary>          /// <param name="dt"></param>          /// <returns></returns>          public static List<T> ConvertToList(DataTable dt)        {            // 定義集合              List<T> ts = new List<T>();            // 獲得此模型的類型              Type type = typeof(T);            //定義一個臨時變數              string tempName = string.Empty;            //遍曆DataTable中所有的資料行              foreach (DataRow dr in dt.Rows)            {                T t = new T();                // 獲得此模型的公用屬性                  PropertyInfo[] propertys = t.GetType().GetProperties();                //遍曆該對象的所有屬性                  foreach (PropertyInfo pi in propertys)                {                    tempName = pi.Name;//將屬性名稱賦值給臨時變數                      //檢查DataTable是否包含此列(列名==對象的屬性名稱)                        if (dt.Columns.Contains(tempName))                    {                        // 判斷此屬性是否有Setter                          if (!pi.CanWrite) continue;//該屬性不可寫,直接跳出                          //取值                          object value = dr[tempName];                        //如果非空,則賦給對象的屬性                          if (value != DBNull.Value)                        {                            // pi.SetValue(t,Convert.ChangeType(value,pi.PropertyType), null);                            if (!pi.PropertyType.IsGenericType)                            {                                //非泛型                                pi.SetValue(t, string.IsNullOrEmpty(value.ToString()) ? null : Convert.ChangeType(value, pi.PropertyType), null);                            }                            else                            {                                //泛型Nullable<>                                Type genericTypeDefinition = pi.PropertyType.GetGenericTypeDefinition();                                if (genericTypeDefinition == typeof(Nullable<>))                                {                                    pi.SetValue(t, string.IsNullOrEmpty(value.ToString()) ? null : Convert.ChangeType(value, Nullable.GetUnderlyingType(pi.PropertyType)), null);                                }                            }                        }                    }                }                //對象添加到泛型集合中                  ts.Add(t);            }            return ts;        }    }}

 

錶轉List泛型數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.