• 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的对象)
        }
  • 相关阅读:
    美国和日本不是盟友吗,为什么拒绝出售F-22战斗机给日本?
    刚刚大学毕业,是选择创业还是选择公司上班?
    现今的社会一定要结婚吗?
    事必躬亲果真是无能的表现?
    课外辅导机构,就让疫情灭了吧!
    这些年,我读书越来越少了!
    Python操作日志、加密、发送邮件、线程、生产者消费者
    Python模块操作之re、MySQL、Excel
    Python面向对象、迭代器、range和切片的区分
    Python模块、文件读写、异常
  • 原文地址:https://www.cnblogs.com/shangdishijiao/p/9145753.html
Copyright © 2020-2023  润新知