zoukankan      html  css  js  c++  java
  • 根据反射解析和转换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;
    
            }
        }
  • 相关阅读:
    计算1的个数
    【环境配置】配置git
    Spoj 9887 Binomial coefficients 构造
    程序猿与HR博弈之:有城府的表达你的兴趣爱好
    C和指针 (pointers on C)——第六章:指针(上)
    关于undo表空间配置错误的ORA-30012
    每天进步一点点——Linux系统中的异常堆栈跟踪简单实现
    javaScript 对象的使用
    手机游戏加密那点事儿_2d资源加密_1
    支持向量机
  • 原文地址:https://www.cnblogs.com/autumoonchina/p/4274318.html
Copyright © 2011-2022 走看看