public static object ToMapFactory()

in Darabonba/ModelExtensions.cs [37:86]


        public static object ToMapFactory(Type type, object value)
        {
            if (value == null)
            {
                return null;
            }
            if (typeof(IList).IsAssignableFrom(type) && !typeof(Array).IsAssignableFrom(type))
            {
                IList list = (IList)value;
                Type listType = type.GetGenericArguments()[0];
                List<object> resultList = new List<object>();
                for (int j = 0; j < list.Count; j++)
                {
                    if (list[j] == null)
                    {
                        resultList.Add(null);
                    }
                    else
                    {
                        resultList.Add(ToMapFactory(listType, list[j]));
                    }
                }
                return resultList;
            }
            else if (typeof(IDictionary).IsAssignableFrom(type))
            {
                IDictionary dic = (IDictionary)value;
                IDictionary resultDic = new Dictionary<string, object>();
                foreach (DictionaryEntry keypair in dic)
                {
                    if (keypair.Value == null)
                    {
                        resultDic.Add(keypair.Key, null);
                    }
                    else
                    {
                        Type valueType = keypair.Value.GetType();
                        resultDic.Add(keypair.Key, ToMapFactory(valueType, keypair.Value));
                    }
                }
                return resultDic;
            }
            else if (typeof(Model).IsAssignableFrom(type))
            {
                Model model = (Model)value;
                return model.ToMap();
            }

            return value;
        }