zoukankan      html  css  js  c++  java
  • C# 相似对象赋值 通过table 互转 另辟蹊径 垃圾简单代码

    using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Text;

    namespace ThinkNet.Utility { ///

    /// List DataTable 转换 ///public class ConvertDataTableToList {

    #region DateTable转List
        /// <summary>
        /// Table转List
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="table"></param>
        /// <returns></returns>
        public static IList<T> ConvertTo<T>(DataTable table)
        {
            if (table == null)
            {
                return null;
            }
    
            List<DataRow> rows = new List<DataRow>();
    
            foreach (DataRow row in table.Rows)
            {
                rows.Add(row);
            }
    
            return ConvertTo<T>(rows);
        }
    
        public static IList<T> ConvertTo<T>(IList<DataRow> rows)
        {
            IList<T> list = null;
    
            if (rows != null)
            {
                list = new List<T>();
    
                foreach (DataRow row in rows)
                {
                    T item = CreateItem<T>(row);
                    list.Add(item);
                }
            }
    
            return list;
        }
    
        public static T CreateItem<T>(DataRow row)
        {
            T obj = default(T);
            if (row != null)
            {
                obj = Activator.CreateInstance<T>();
    
                foreach (DataColumn column in row.Table.Columns)
                {
                    PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
                    try
                    {
                        object value = row[column.ColumnName];
                        prop.SetValue(obj, value, null);
                    }
                    catch
                    {  //You can log something here     
                        //throw;    
                    }
                }
            }
    
            return obj;
        }
        #endregion
    
        #region  反射List To DataTable
        /// <summary>
        /// 将List集合类转换成DataTable 
        /// </summary>
        /// <param name="list">集合 </param>
        /// <returns>DataTable</returns>
        public static DataTable ListToDataTable(IList list)
        {
            DataTable result = new DataTable();
            if (list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    //获取类型
                    Type colType = pi.PropertyType;
                    //当类型为Nullable<>时
                    if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                    {
                        colType = colType.GetGenericArguments()[0];
                    }
                    result.Columns.Add(pi.Name, colType);
                }
                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;
        }
        #endregion
    }

    }

     

    调用

     
    public List<Sal_SalesOrderLineEx> Get_ListSal_SalesOrderLineEx(List<Sal_SalesOrderLine> Listline)
        {
            DataTable table = ConvertDataTableToList.ListToDataTable(_ListSal_SalesOrderLine);
            IList<Sal_SalesOrderLineEx> list = ConvertDataTableToList.ConvertTo<Sal_SalesOrderLineEx>(table);
            return list.ToList<Sal_SalesOrderLineEx>();
            //  AutoMapper.Mapper.DynamicMap<类A, 类B>(类型A的对象)
        }
  • 相关阅读:
    3d服务器配置
    Can't connect to postgres on centos with psycopg
    flask快速入门
    nohup: cannot run command “/bin/java”:
    linux 上redis的启动口令
    CentOS网络设置 couldn't resolve host 'mirrorlist.centos.org问题解决
    CentOS下使用Mysql
    解决nodejs跨域的一个中间件
    JS实现禁用滑动条但滑动条不消失的效果
    JQ实现下拉加载更多
  • 原文地址:https://www.cnblogs.com/shangdishijiao/p/9145753.html
Copyright © 2011-2022 走看看