Beispiel #1
0
        /// <summary>
        /// 实体转成Json
        /// </summary>
        /// <param name="value">实体</param>
        /// <param name="propertyMap">Key的名字</param>
        /// <param name="formatValue">格式化值的方法</param>
        /// <returns></returns>
        public static string SerializeObject(object value,
                                             IEnumerable <string> propertyMap, DelFormatValue formatValue)
        {
            Dictionary <string, object> lst = EntitySerializer.SerializeToDictionary(value, propertyMap, formatValue);

            return(SerializeObject(lst));
        }
Beispiel #2
0
        /// <summary>
        /// 实体集合转成Json
        /// </summary>
        /// <param name="lstEntity">实体</param>
        /// <param name="propertyMap">Key的名字</param>
        /// <param name="formatValue">格式化值的方法</param>
        /// <returns></returns>
        public static string SerializeList(IList lstEntity,
                                           IEnumerable <string> propertyMap, DelFormatValue formatValue)
        {
            List <Dictionary <string, object> > lst = EntitySerializer.SerializeListToDictionary(lstEntity, propertyMap, formatValue);

            return(SerializeList(lst));
        }
Beispiel #3
0
        /// <summary>
        /// 实体转换为字典集合(适合JavaScriptSerializer)
        /// </summary>
        /// <param name="lstEntity">实体</param>
        /// <param name="propertyCollection">需要对应的字典键值(如:new string[]{"user=User.Name","uid=UserId"})</param>
        /// <param name="formatValue">格式化值的方法</param>
        /// <returns></returns>
        public static Dictionary <string, object> SerializeToDictionary(object entity,
                                                                        IEnumerable <string> propertyCollection, DelFormatValue formatValue)
        {
            if (entity == null)
            {
                return(new Dictionary <string, object>());
            }

            Type objType = entity.GetType();
            List <EntitySerializerInfo> lstInfo = null;

            if (propertyCollection == null)
            {
                lstInfo = GetTypeInfos(objType);
            }
            else
            {
                lstInfo = GetDicInfos(objType, propertyCollection);
            }
            object value = null;


            Dictionary <string, object> item = new Dictionary <string, object>();

            foreach (EntitySerializerInfo info in lstInfo)
            {
                value = GetValue(info, entity);
                if (formatValue != null)
                {
                    value = formatValue(info.Name, info.PropertyName, entity, value);
                }
                item[info.Name] = value;
            }
            return(item);
        }
Beispiel #4
0
        /// <summary>
        /// 把集合信息变成实体集合
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="dic">信息集合</param>
        /// <param name="propertyCollection">需要对应的字典键值(如:new string[]{"user=User.Name","uid=UserId"})</param>
        /// <returns></returns>
        public static List <T> DeserializeToList <T>(List <Dictionary <string, object> > lstDic, IEnumerable <string> propertyCollection, DelFormatValue formatValue)
            where T : new()
        {
            List <T> lstRet = new List <T>();

            if (lstDic == null)
            {
                return(lstRet);
            }

            Type objType = typeof(T);

            List <EntitySerializerInfo> lstInfo = null;

            if (propertyCollection == null)
            {
                lstInfo = GetTypeInfos(objType);
            }
            else
            {
                lstInfo = GetDicInfos(objType, propertyCollection);
            }

            object value = null;

            foreach (Dictionary <string, object> dic in lstDic)
            {
                T entity = (T)Activator.CreateInstance(objType);

                foreach (EntitySerializerInfo info in lstInfo)
                {
                    if (!dic.TryGetValue(info.Name, out value))
                    {
                        continue;
                    }
                    value = SetValue(info, entity, value);
                    if (formatValue != null)
                    {
                        value = formatValue(info.Name, info.PropertyName, entity, value);
                    }
                }
                lstRet.Add(entity);
            }
            return(lstRet);
        }
Beispiel #5
0
        /// <summary>
        /// 实体转换为DataTable
        /// </summary>
        /// <param name="lstEntity">实体</param>
        /// <param name="propertyCollection">需要对应的字典键值(如:new string[]{"user=User.Name","uid=UserId"})</param>
        /// <param name="formatValue">格式化值的方法</param>
        /// <returns></returns>
        public static DataTable SerializeListToDataTable(IList lstEntity,
                                                         IEnumerable <string> propertyCollection, DelFormatValue formatValue)
        {
            if (lstEntity.Count == 0)
            {
                return(new DataTable());
            }
            DataTable dt      = new DataTable("DS");
            Type      objType = lstEntity[0].GetType();
            List <EntitySerializerInfo> lstInfo = null;

            if (propertyCollection == null)
            {
                lstInfo = GetTypeInfos(objType);
            }
            else
            {
                lstInfo = GetDicInfos(objType, propertyCollection);
            }

            foreach (EntitySerializerInfo info in lstInfo)
            {
                DataColumn dc = new DataColumn(info.Name, info.PropertyInfos[info.PropertyInfos.Count - 1].PropertyType);
                dt.Columns.Add(dc);
            }
            object value = null;

            dt.BeginLoadData();
            foreach (object obj in lstEntity)
            {
                DataRow dr = dt.NewRow();
                foreach (EntitySerializerInfo info in lstInfo)
                {
                    value = GetValue(info, obj);
                    if (formatValue != null)
                    {
                        value = formatValue(info.Name, info.PropertyName, obj, value);
                    }
                    dr[info.Name] = value;
                }
                dt.Rows.Add(dr);
            }
            dt.EndLoadData();
            return(dt);
        }
Beispiel #6
0
        /// <summary>
        /// 实体转换为字典集合(适合JavaScriptSerializer)
        /// </summary>
        /// <param name="lstEntity">实体</param>
        /// <param name="propertyCollection">需要对应的字典键值(如:new string[]{"user=User.Name","uid=UserId"})</param>
        /// <param name="formatValue">格式化值的方法</param>
        /// <returns></returns>
        public static List <Dictionary <string, object> > SerializeListToDictionary(IList lstEntity,
                                                                                    IEnumerable <string> propertyCollection, DelFormatValue formatValue)
        {
            if (lstEntity.Count == 0)
            {
                return(new List <Dictionary <string, object> >());
            }
            List <Dictionary <string, object> > lstDic = new List <Dictionary <string, object> >(lstEntity.Count);
            Type objType = lstEntity[0].GetType();
            List <EntitySerializerInfo> lstInfo = null;

            if (propertyCollection == null)
            {
                lstInfo = GetTypeInfos(objType);
            }
            else
            {
                lstInfo = GetDicInfos(objType, propertyCollection);
            }
            object value = null;

            foreach (object obj in lstEntity)
            {
                Dictionary <string, object> item = new Dictionary <string, object>();
                foreach (EntitySerializerInfo info in lstInfo)
                {
                    value = GetValue(info, obj);
                    if (formatValue != null)
                    {
                        value = formatValue(info.Name, info.PropertyName, obj, value);
                    }
                    item[info.Name] = value;
                }
                lstDic.Add(item);
            }
            return(lstDic);
        }
Beispiel #7
0
        /// <summary>
        /// 值集合转换为字典集合(适合JavaScriptSerializer)
        /// </summary>
        /// <param name="lstEntity">实体</param>
        /// <param name="propertyMap">Key的名字</param>
        /// <param name="formatValue">格式化值的方法</param>
        /// <returns></returns>
        public static List <Dictionary <string, object> > SerializeListToDictionary(IList lstValue,
                                                                                    string propertyMap, DelFormatValue formatValue)
        {
            if (lstValue.Count == 0)
            {
                return(new List <Dictionary <string, object> >());
            }
            List <Dictionary <string, object> > lstDic = new List <Dictionary <string, object> >(lstValue.Count);
            object value = null;

            foreach (object obj in lstValue)
            {
                Dictionary <string, object> item = new Dictionary <string, object>();
                value = obj;
                if (formatValue != null)
                {
                    value = formatValue(propertyMap, "value", value, value);
                }
                item[propertyMap] = value;
                lstDic.Add(item);
            }
            return(lstDic);
        }
Beispiel #8
0
        /// <summary>
        /// 把集合信息变成实体集合
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="dic">信息集合</param>
        /// <param name="propertyCollection">需要对应的字典键值(如:new string[]{"user=User.Name","uid=UserId"})</param>
        /// <returns></returns>
        public static T DeserializeToObject <T>(Dictionary <string, object> dic, IEnumerable <string> propertyCollection, DelFormatValue formatValue)
            where T : new()
        {
            if (dic == null)
            {
                return(default(T));
            }

            Type objType = typeof(T);
            T    entity  = (T)Activator.CreateInstance(objType);

            DeserializeToObject(entity, dic, propertyCollection, formatValue);

            return(entity);
        }
Beispiel #9
0
        /// <summary>
        /// 把集合信息变成实体集合
        /// </summary>
        /// <typeparam name="T">实体</typeparam>
        /// <param name="dic">信息集合</param>
        /// <param name="propertyCollection">需要对应的字典键值(如:new string[]{"user=User.Name","uid=UserId"})</param>
        /// <returns></returns>
        public static void DeserializeToObject(object obj, Dictionary <string, object> dic, IEnumerable <string> propertyCollection, DelFormatValue formatValue)
        {
            Type objType = obj.GetType();
            List <EntitySerializerInfo> lstInfo = null;

            if (propertyCollection == null)
            {
                lstInfo = GetTypeInfos(objType);
            }
            else
            {
                lstInfo = GetDicInfos(objType, propertyCollection);
            }

            object value = null;
            Dictionary <string, object> item = new Dictionary <string, object>();

            foreach (EntitySerializerInfo info in lstInfo)
            {
                if (!dic.TryGetValue(info.Name, out value))
                {
                    continue;
                }
                value = SetValue(info, obj, value);
                if (formatValue != null)
                {
                    value = formatValue(info.Name, info.PropertyName, obj, value);
                }
                item[info.Name] = value;
            }
        }