• 根据反射解析和转换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;
    
            }
        }
  • 相关阅读:
    django 添加字段, migrate时候生成一个默认值
    django 开发经验
    GeoIP2-python
    一些好用的django模块
    ubuntu linux samba
    动画工作室
    以太坊dApp全栈开发教程(引言)
    社交网络去中心化已成趋势,读懂核心底层技术选择和路线
    CryptoCurrency Security Standard (CCSS)
    HARNESS THE POWER OF DISTRIBUTED STORAGE IPFS AND SWARM IN ETHEREUM BLOCKCHAIN APPLICATIONS
  • 原文地址:https://www.cnblogs.com/autumoonchina/p/4274318.html
Copyright © 2020-2023  润新知