Example #1
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));
        }
Example #2
0
        /// <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);
        }