zoukankan      html  css  js  c++  java
  • c# .NET DataTable转换为List 反射

    public static List<T> ConvertToEx<T>(DataTable dt) where T : new()
            {
                if (dt == null) return null;
                if (dt.Rows.Count <= 0) return null;
    
                List<T> list = new List<T>();
                Type type = typeof(T);
                PropertyInfo[] propertyInfos = type.GetProperties();  //获取泛型的属性
                List<DataColumn> listColumns = dt.Columns.Cast<DataColumn>().ToList();  //获取数据集的表头,以便于匹配
                T t;
                foreach (DataRow dr in dt.Rows)
                {
                    t = new T();
                    foreach (PropertyInfo propertyInfo in propertyInfos)
                    {
                        try
                        {
                            DataColumn dColumn = listColumns.Find(name => name.ToString().ToUpper() == propertyInfo.Name.ToUpper());  //查看是否存在对应的列名
    
                            if (dColumn != null)
                            {
                                var uu = propertyInfo.PropertyType.Name;
                                if (uu == "Int32")
                                {
                                    if (dr[propertyInfo.Name].ToString() != "")
                                    {
                                        propertyInfo.SetValue(t, Convert.ToInt32(dr[propertyInfo.Name]), null);  //赋值
                                    }
                                }
                                else if (uu == "Boolean")
                                {
                                    if (dr[propertyInfo.Name].ToString() != "")
                                    {
                                        propertyInfo.SetValue(t, Convert.ToBoolean(dr[propertyInfo.Name]), null);  //赋值
                                    }
                                }
                                else if (uu == "Decimal")
                                {
                                    if (dr[propertyInfo.Name].ToString() != "")
                                    {
                                        propertyInfo.SetValue(t, Convert.ToDecimal(dr[propertyInfo.Name]), null);  //赋值
                                    }
                                }
                                else if (uu == "Double")
                                {
                                    if (dr[propertyInfo.Name].ToString() != "")
                                    {
                                        propertyInfo.SetValue(t, Convert.ToDouble(dr[propertyInfo.Name]), null);  //赋值
                                    }
                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(dr[propertyInfo.Name].ToString()))
                                    {
                                        propertyInfo.SetValue(t, dr[propertyInfo.Name], null);  //赋值
                                    }
                                }
    
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(ex.Message);
                        }
                    }
                    list.Add(t);
                }
                return list;
            }
  • 相关阅读:
    FCKEditor使用说明
    如何查看索引或table所佔用的實際空間
    java字节输入流
    文件的读写操作
    利用JProfiler
    javascript面向对象技术基础(五)
    深刻理解Linux进程间通信(IPC)
    JAVA中操作数据库方式与设计模式的应用 2
    javascript面向对象技术基础(六)
    FCKeditor在线编辑器
  • 原文地址:https://www.cnblogs.com/wangxlei/p/15331803.html
Copyright © 2011-2022 走看看