根据反射解析和转换DataRow

根据反射解析和转换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;

        }
    }

 

posted @ 2015-02-05 11:23  秋月的私语  阅读(477)  评论(0编辑  收藏  举报