示例#1
0
        /// <summary>
        /// 将 datatable 转换为 list
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="table"></param>
        /// <returns></returns>
        public static List <T> ToList <T>(this DataTable table) where T : class, new()
        {
            var list = new List <T>();

            var _Entity      = ReflexHelper.CreateInstance <T>();
            var propertyInfo = _Entity.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

            foreach (DataRow dr in table.Rows)
            {
                _Entity = ReflexHelper.CreateInstance <T>();
                foreach (var item in propertyInfo)
                {
                    string AttrName = item.Name;
                    foreach (DataColumn dc in dr.Table.Columns)
                    {
                        if (AttrName != dc.ColumnName)
                        {
                            continue;
                        }
                        if (dr[dc.ColumnName] != DBNull.Value)
                        {
                            item.SetValue(_Entity, dr[dc.ColumnName], null);
                        }
                        else
                        {
                            item.SetValue(_Entity, null, null);
                        }
                    }
                }
                list.Add(_Entity);
            }
            return(list);
        }
示例#2
0
        /// <summary>
        /// DataRow 转换 实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static T ToEntity <T>(this DataRow dr) where T : class, new()
        {
            var _Entity = ReflexHelper.CreateInstance <T>();
            var list    = _Entity.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

            if (list.Length == 0)
            {
                throw new Exception("找不到任何 公共属性!");
            }

            foreach (var item in list)
            {
                string AttrName = item.Name;
                foreach (DataColumn dc in dr.Table.Columns)
                {
                    if (AttrName != dc.ColumnName)
                    {
                        continue;
                    }
                    if (dr[dc.ColumnName] != DBNull.Value)
                    {
                        item.SetValue(_Entity, dr[dc.ColumnName], null);
                    }
                }
            }
            return(_Entity);
        }
示例#3
0
        /// <summary>
        /// 对象 转换为 MemberInitExpression
        /// </summary>
        /// <param name="_type"></param>
        /// <returns></returns>
        public static MemberInitExpression MemberInit <T>(T _entity)
        {
            var proInfo = ReflexHelper.GetPropertyInfos(typeof(T));

            var list = new List <MemberBinding>();

            foreach (var item in proInfo)
            {
                list.Add(Expression.Bind(item, Expression.Constant(item.GetValue(_entity), item.PropertyType)));
            }

            var newExpr = Expression.New(typeof(T));

            return(Expression.MemberInit(newExpr, list));
        }
示例#4
0
文件: Tools.cs 项目: sumbit/model
        /// <summary>
        /// 将多个实体组合成为一个 字典类型
        /// </summary>
        /// <param name="di"></param>
        /// <returns></returns>
        public static Dictionary <string, object> EntityToDictionary(Dictionary <string, object> di)
        {
            Dictionary <string, object> r = new Dictionary <string, object>();

            foreach (var item in di)
            {
                Console.WriteLine(item.Value.GetType());
                Console.WriteLine(item.Value.GetType().Namespace);
                if (item.Value.GetType().Namespace == "ADT.Models")
                {
                    ReflexHelper.GetPropertyInfos(item.Value.GetType()).ToList().ForEach(pi =>
                    {
                        if (pi.GetValue(item.Value, null) == null)
                        {
                            r.Add(pi.Name, null);
                        }
                        else
                        {
                            if (pi.PropertyType == typeof(DateTime))
                            {
                                r.Add(pi.Name, pi.GetValue(item.Value, null).ToDateTimeFormat("yyyy-MM-dd HH:mm:ss"));
                            }
                            else
                            {
                                r.Add(pi.Name, pi.GetValue(item.Value, null));
                            }
                        }
                    });
                }
                else
                {
                    r.Add(item.Key, item.Value);
                }
            }
            return(r);
        }
示例#5
0
 /// <summary>
 /// 获取 对象 中 某个属性得 标记
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="_type"></param>
 /// <param name="_name"></param>
 /// <returns></returns>
 public static T GetAttribute <T>(Type _type, string _name) where T : Attribute
 {
     return(ReflexHelper.GetPropertyInfo(_type, _name).GetCustomAttribute(typeof(T)) as T);
 }
示例#6
0
 /// <summary>
 /// 获取 对象 属性上 标记 集合
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="_type"></param>
 /// <param name="_name"></param>
 /// <returns></returns>
 public static List <T> GetAttributes <T>(Type _type, string _name) where T : Attribute
 {
     return(ReflexHelper.GetPropertyInfo(_type, _name).GetCustomAttributes <T>(false).ToList());
 }
示例#7
0
 /// <summary>
 /// 设置 值
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="_entity"></param>
 /// <param name="_name"></param>
 /// <param name="_val"></param>
 public static void SetValue <T>(T _entity, string _name, object _val)
 {
     ReflexHelper.GetPropertyInfo(typeof(T), _name).SetValue(_entity, _val);
 }
示例#8
0
 /// <summary>
 /// 获取 值
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="_entity"></param>
 /// <param name="_name"></param>
 /// <returns></returns>
 public static object GetValue <T>(T _entity, string _name)
 {
     return(ReflexHelper.GetPropertyInfo(typeof(T), _name).GetValue(_entity));
 }