曹永思

导航

反射List<M> To DataTable|反射IList To DataTable|反射 DataTable To List<M>

 将DataTable集合反射获取 List<M>

 /// <summary>
        /// 根据DataTable集合反射获取 List<M>
        /// </summary>
        /// <typeparam name="M">泛型实体</typeparam>
        /// <param name="dt">DataTable</param>
        /// <returns>实体集合</returns>
        private static List<M> SetValueRow<M>(DataTable dt) where M : new()
        {
            List<M> list = new List<M>();

            Type type;
            PropertyInfo p;
            M m;

            foreach (DataRow row in dt.Rows)
            {
                m = new M();
                type = m.GetType();

                foreach (DataColumn col in dt.Columns)
                {
                    //获取一个字段的属性
                    p = type.GetProperty(col.ColumnName);

                    //实体中无对应属性
                    if (p == null)
                        continue;

                    string colDbType = row[col.ColumnName].GetType().FullName;

                    //结果集单元格中的值不为空时才赋值
                    if (colDbType != "System.DBNull")
                    {
                        switch (p.PropertyType.FullName)
                        {
                            case "System.Int64"://根据不同数据库数据类型作转换,如oracle的number(2)应转换为Int32,而不是默认的Decemal 
                                p.SetValue(m, Convert.ToInt64(row[col.ColumnName]), null);
                                break;

                            case "System.Int32":
                                p.SetValue(m, Convert.ToInt32(row[col.ColumnName]), null);
                                break;

                            case "System.Int16":
                                p.SetValue(m, Convert.ToInt16(row[col.ColumnName]), null);
                                break;

                            case "System.String":
                                p.SetValue(m, Convert.ToString(row[col.ColumnName]), null);
                                break;

                            case "System.Decimal":
                                p.SetValue(m, Convert.ToDecimal(row[col.ColumnName]), null);
                                break;

                            case "System.DateTime":
                                p.SetValue(m, row[col.ColumnName], null);
                                break;

                            case "System.Double":
                                p.SetValue(m, Convert.ToDouble(row[col.ColumnName]), null);
                                break;

                            case "System.Boolean":
                                p.SetValue(m, Convert.ToBoolean(row[col.ColumnName]), null);
                                break;

                            case "System.Byte":
                                p.SetValue(m, Convert.ToByte(row[col.ColumnName]), null);
                                break;

                            default:
                                p.SetValue(m, row[col.ColumnName], null);
                                break;
                        }
                    }
                }

                list.Add(m);
            }

            return list;
        }
        
View Code

 将IList集合类转换成DataTable  

/// <summary>  
        /// 将IList集合类转换成DataTable  
        /// </summary>  
        /// <param name="list">集合</param>  
        /// <returns></returns>  
        public static DataTable IListToDataTable(IList list)
        {
            DataTable result = new DataTable();
            if (list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    result.Columns.Add(pi.Name, pi.PropertyType);
                }

                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(list[i], null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    result.LoadDataRow(array, true);
                }
            }
            return result;
        }
View Code
将List<M>集合类转换成DataTable 
        /// <summary>  
        /// 将List<M>集合类转换成DataTable  
        /// </summary>  
        /// <param name="list">集合</param>  
        /// <returns></returns>  
        public static DataTable IListToDataTable<M>(List<M> list)
        {
            DataTable result = new DataTable();
            if (list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    result.Columns.Add(pi.Name, pi.PropertyType);
                }

                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(list[i], null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    result.LoadDataRow(array, true);
                }
            }
            return result;
        }
View Code

全局静态类 泛型方法
/// <summary>
/// 全局静态类
/// </summary>
public static class GlobalStaticClass : Object
{

    public static List<M> ToModelList<M>(this object obj) where M : new()
    {
        List<M> list = new List<M>();

        Type type;
        PropertyInfo p;
        M m;
        DataTable dt = (DataTable)obj;

        foreach (DataRow row in dt.Rows)
        {
            m = new M();
            type = m.GetType();

            foreach (DataColumn col in dt.Columns)
            {
                //获取一个字段的属性
                p = type.GetProperty(col.ColumnName);

                //实体中无对应属性
                if (p == null)
                    continue;

                string colDbType = row[col.ColumnName].GetType().FullName;

                //结果集单元格中的值不为空时才赋值
                if (colDbType != "System.DBNull")
                {
                    switch (p.PropertyType.FullName)
                    {
                        case "System.Int64"://根据不同数据库数据类型作转换,如oracle的number(2)应转换为Int32,而不是默认的Decemal 
                            p.SetValue(m, Convert.ToInt64(row[col.ColumnName]), null);
                            break;

                        case "System.Int32":
                            p.SetValue(m, Convert.ToInt32(row[col.ColumnName]), null);
                            break;

                        case "System.Int16":
                            p.SetValue(m, Convert.ToInt16(row[col.ColumnName]), null);
                            break;

                        case "System.String":
                            p.SetValue(m, Convert.ToString(row[col.ColumnName]), null);
                            break;

                        case "System.Decimal":
                            p.SetValue(m, Convert.ToDecimal(row[col.ColumnName]), null);
                            break;

                        case "System.DateTime":
                            p.SetValue(m, row[col.ColumnName], null);
                            break;

                        case "System.Double":
                            p.SetValue(m, Convert.ToDouble(row[col.ColumnName]), null);
                            break;

                        case "System.Boolean":
                            p.SetValue(m, Convert.ToBoolean(row[col.ColumnName]), null);
                            break;

                        case "System.Byte":
                            p.SetValue(m, Convert.ToByte(row[col.ColumnName]), null);
                            break;

                        default:
                            p.SetValue(m, row[col.ColumnName], null);
                            break;
                    }
                }
            }

            list.Add(m);
        }

        return list;
    }

}
View Code

 

实体父类,实体继承此类后,实体对象可调用this.SetValue(object) 方法通过反射给自身对象赋值

    public class ModelBase
    {
        protected bool isNull = true;
        public bool IsNull
        {
            get { return isNull; }
            set { isNull = value; }
        }
        protected void SetValue(object info)
        {
            foreach (FieldInfo fi in info.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
            {
                this.GetType().GetField(fi.Name, BindingFlags.NonPublic | BindingFlags.Instance).SetValue(this, fi.GetValue(info));
            }
        }
        public void SetValue(SqlDataReader dr)
        {
            if (dr.Read())
            {
                foreach (FieldInfo fi in this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
                {
                    if (fi.Name != "isNull")
                    {
                        object rel = dr[fi.Name];
                        if (dr[fi.Name] != Convert.DBNull)
                        {
                            fi.SetValue(this, dr[fi.Name]);
                        }

                    }
                }
                this.isNull = false;
            }
            dr.Close();
        }
        public void SetValue(DataRow dr)
        {
            foreach (FieldInfo fi in this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if (fi.Name != "isNull")
                {
                    if (dr.Table.Columns.Contains(fi.Name))
                    {
                        object rel = dr[fi.Name];
                        if (dr[fi.Name] != Convert.DBNull)
                        {
                            fi.SetValue(this, dr[fi.Name]);
                        }
                    }
                }
            }
            this.isNull = false;

        }



    }
View Code

 




posted on 2014-09-01 10:17  曹永思  阅读(308)  评论(0编辑  收藏  举报