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

根据反射解析和转换DataRow

时间:2015-02-05 12:52:12      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

根据反射解析和转换DataRow。

    public class DataConvert
    {
        //获取文件结构
        //dataRow转T 
        public static T GetModelByDataRow<T>(System.Data.DataRow dr) where T : new()
        {
            T model = new T();
            foreach (PropertyInfo pInfo in model.GetType().GetProperties())
            {
                object val = getValueByColumnName(dr, pInfo.Name);
                pInfo.SetValue(model, val, null);
            }
            return model;
        }
        // T转dataRow

        public static DataRow GetDataRowByModel<T>(T _t, DataTable dt)
        {
            DataRow dr = dt.NewRow();
            ArrayList tempList = new ArrayList();
            foreach (PropertyInfo pi in _t.GetType().GetProperties())
            {
                object obj = pi.GetValue(_t, null);
                tempList.Add(obj);
            }
            object[] array = tempList.ToArray();
            dr.ItemArray = array;
            return dr;
        }

        //返回DataRow 中对应的列的值。  
        public static object getValueByColumnName(System.Data.DataRow dr, string columnName)
        {
            if (dr.Table.Columns.IndexOf(columnName) >= 0)
            {
                if (dr[columnName] == DBNull.Value)
                    return null;
                return dr[columnName];
            }
            return null;
        }


        //数据转换
        public static IList<T> readTxt2List<T>(FileInfo[] files, char charSplit, string[] defStrHeader)
        {
            IList<T> poslist = new List<T>();
            for (int j = 0; j < files.Length; j++)
            {
                FileStream fs = new FileStream(files[j].FullName, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                string mytext = "";
                string[] sHeader;
                if (defStrHeader != null)
                {
                    sHeader = defStrHeader;
                }
                else
                {
                    sHeader = sr.ReadLine().Split(charSplit);

                }
                Dictionary<string, int> headerDic = new Dictionary<string, int>();
                for (int i = 0; i < sHeader.Length; i++)
                {
                    headerDic.Add(sHeader[i], i);

                }

                while ((mytext = sr.ReadLine()) != null)
                {
                    if (mytext.Trim() == "")
                    {
                        continue;
                    }
                    string[] sArray = mytext.Split(charSplit);
                    T _t = (T)Activator.CreateInstance(typeof(T));
                    PropertyInfo[] propertys = _t.GetType().GetProperties();
                    foreach (var pi in propertys)
                    {
                        if (headerDic.ContainsKey(pi.Name))
                        {
                            if (headerDic[pi.Name] > sArray.Length - 1)
                            {
                                continue;
                            }
                            pi.SetValue(_t, sArray[headerDic[pi.Name]], null);
                            continue;
                        }

                    }
                    poslist.Add(_t);
                }
            }
            return poslist;

        }
    }

 

根据反射解析和转换DataRow

标签:

原文地址:http://www.cnblogs.com/autumoonchina/p/4274318.html

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