Beispiel #1
0
        /// <summary>
        /// Convert strong-typed entity to map presentation for transfer
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="o"></param>
        /// <param name="fields"></param>
        /// <returns></returns>
        public static IDictionary <string, object> Convert <T>(T o, IList <FormField> fields)
        {
            IDictionary <string, object> data = null;

            if (null == fields)
            {
                return(data);
            }
            int min = fields.Select(f => InfoPlusEntity.CalculateGroupDepth(f.GroupName)).Min();
            var g0  = fields.Where(f => InfoPlusEntity.CalculateGroupDepth(f.GroupName) == min)
                      .Select(f => f.GroupName).First();

            InfoPlusEntity.Convert(o, fields, g0, null, 0, ref data);
            return(data);
        }
Beispiel #2
0
        static void Convert(object o, IList <FormField> fields, string groupPath, string dataPath, int depth, ref IDictionary <string, object> data)
        {
            if (null == o)
            {
                return;
            }
            if (null == dataPath)
            {
                dataPath = string.Empty;
            }
            Type type = o.GetType();

            if (null == data)
            {
                data = new Dictionary <string, object>();
            }

            // current level, read property directly.
            // should always be equal, Only, except the initial call.
            string[] groups = groupPath.Split(new string[] { "//" }, StringSplitOptions.RemoveEmptyEntries);
            if (groups.Length > depth + 1)
            {
                string       gn       = InfoPlusEntity.ParseGroupName(groups[depth + 1]);
                PropertyInfo property = type.GetProperty(gn);
                if (null == property)
                {
                    return;
                }
                object deeps = property.GetValue(o, null);
                Array  arr   = InfoPlusEntity.ObjectToArray(deeps);
                if (null == arr)
                {
                    return;
                }
                for (int n = 0; n < arr.Length; n++)
                {
                    object deep = arr.GetValue(n);
                    InfoPlusEntity.Convert(deep, fields, groupPath, dataPath + "/" + n, depth + 1, ref data);
                }
            }
            else
            {
                var currentFields = fields.Where(f => f.GroupName == groupPath);
                foreach (FormField field in currentFields)
                {
                    string propertyName, fieldName;
                    InfoPlusEntity.ParsePropertyName(field.Name, out propertyName, out fieldName);
                    PropertyInfo property = type.GetProperty(propertyName);
                    if (null == property)
                    {
                        continue;
                    }

                    object val = property.GetValue(o, null);
                    InfoPlusEntity.FormDataAssign(fieldName, val, data, dataPath);
                }

                var deepers = from f in fields
                              where InfoPlusEntity.CalculateGroupDepth(f.GroupName) > depth && f.GroupName.Contains(groupPath)
                              select f;

                if (deepers.Count() > 0)
                {
                    int min        = deepers.Select(f => InfoPlusEntity.CalculateGroupDepth(f.GroupName)).Min();
                    var groupNames = deepers.Where(f => InfoPlusEntity.CalculateGroupDepth(f.GroupName) == min)
                                     .Select(f => f.GroupName).Distinct().ToList();

                    foreach (string groupName in groupNames)
                    {
                        string[] gns = groupName.Split(new string[] { "//" }, StringSplitOptions.RemoveEmptyEntries);

                        if (depth < gns.Length - 1)
                        {
                            string       gn       = InfoPlusEntity.ParseGroupName(gns[depth + 1]);
                            PropertyInfo property = type.GetProperty(gn);
                            if (null == property)
                            {
                                continue;
                            }
                            object deeps = property.GetValue(o, null);
                            Array  arr   = InfoPlusEntity.ObjectToArray(deeps);
                            if (null == arr)
                            {
                                continue;
                            }
                            // assign inner field to array in case of empty.
                            if (0 == arr.Length)
                            {
                                var groupFields = deepers.Where(f => f.GroupName == groupName);
                                foreach (var field in groupFields)
                                {
                                    InfoPlusEntity.FormDataAssign(field.Name, new Array[0], data, dataPath);
                                }
                            }

                            for (int n = 0; n < arr.Length; n++)
                            {
                                object deep = arr.GetValue(n);
                                InfoPlusEntity.Convert(deep, fields, groupName, dataPath + "/" + n, depth + 1, ref data);
                            }
                        }
                    }
                }
            }
        }