zoukankan      html  css  js  c++  java
  • 将datarow转换为实体的方法

    /// <summary>
            /// 将DataRow的值赋值到实体对象中.
            /// </summary>
            /// <typeparam name="T">实体对象烈性</typeparam>
            /// <param name="row">DataRow对象</param>
            /// <returns>赋值后的实体对象</returns>
            public static T ToEntity<T>(this DataRow row) where T : class, new()
            {
                if (row == null) return default(T);
                T result = new T();

                Type type = result.GetType();
                PropertyInfo[] properties = type.GetProperties();

                foreach (DataColumn column in row.Table.Columns)
                {
                    var tmpPropQuery = from item in properties
                                       where item.Name == column.ColumnName
                                       select item;
                    if (tmpPropQuery.Count() > 0)
                    {
                        PropertyInfo tmpProp = tmpPropQuery.FirstOrDefault();
                        if (tmpProp != null && tmpProp.CanWrite)
                        {
                            object o = row[column.ColumnName] != DBNull.Value && row[column.ColumnName] != null ?
                                row[column.ColumnName] : null;

                            object[] paras = { o };
                            tmpProp.ReflectedType.InvokeMember(tmpProp.Name, BindingFlags.SetProperty, null, result, paras);
                        }
                    }
                }

                return result;
            }

  • 相关阅读:
    perl get请求发送json数据
    一招教你玩转SQL:通过找出数据的共同属性实现SQL需求
    Uncaught (in promise) Error: Component series.map not exists. Load it first
    python post 参数
    python 发送json数据
    毫秒级从百亿大表任意维度筛选数据,是怎么做到的…
    python 日期处理
    python urlencode urldecode
    在服务器本地监控服务端口命令之ss
    在服务器本地监控服务端口命令之ss
  • 原文地址:https://www.cnblogs.com/eryang/p/2563777.html
Copyright © 2011-2022 走看看