Esempio n. 1
0
        private static IList ToList(IDictionary dictionary)
        {
            Type[] typeArguments = TypeInterrogator.GetItemTypes(dictionary.GetType());
            Type   lstkvType     = GenBuilder.BuildType(typeof(KVPair <,>), typeArguments);
            IList  target        = (IList)GenBuilder.BuildInstance(typeof(List <>), new Type[] { lstkvType });

            foreach (DictionaryEntry it in dictionary)
            {
                target.Add(GenBuilder.BuildInstance(lstkvType, new object[] { it.Key, it.Value }));
            }

            return(target);
        }
Esempio n. 2
0
        public static void Save <T>(string fileName, T obj)
        {
            Type objType = typeof(T);

            if (objType == typeof(DataTable))
            {
                DoSave(fileName, ToDataSet((DataTable)(object)obj));
            }
            else if (TypeInterrogator.IsDictionaryType(objType))
            {
                DoSave(fileName, ToList((IDictionary)obj));
            }
            else
            {
                DoSave(fileName, obj);
            }
        }
Esempio n. 3
0
        public static T Load <T>(string fileName)
        {
            Type objType = typeof(T);

            if (objType == typeof(DataTable))
            {
                DataSet ds = (DataSet)DoLoad(fileName, typeof(DataSet));
                return((T)((object)ToDataTable(ds)));
            }
            else if (TypeInterrogator.IsDictionaryType(objType))
            {
                return((T)((object)LoadDictionary(fileName, objType)));
            }
            else
            {
                return((T)DoLoad(fileName, objType));
            }
        }
Esempio n. 4
0
        private static IDictionary LoadDictionary(string fileName, Type dictionaryT)
        {
            Type[] typeArguments   = TypeInterrogator.GetItemTypes(dictionaryT);
            Type   lstKeyValueType = GenBuilder.BuildType(typeof(KVPair <,>), typeArguments);
            Type   lstType         = GenBuilder.BuildType(typeof(List <>), new Type[] { lstKeyValueType });
            IList  list            = (IList)DoLoad(fileName, lstType);

            PropertyInfo keyProperty   = lstKeyValueType.GetProperty("Key");
            PropertyInfo valueProperty = lstKeyValueType.GetProperty("Value");
            IDictionary  target        = (IDictionary)GenBuilder.BuildInstance(typeof(Dictionary <,>), typeArguments);

            foreach (object it in list)
            {
                object key = keyProperty.GetValue(it, null);
                if (key != null)
                {
                    target.Add(key, valueProperty.GetValue(it, null));
                }
            }

            return(target);
        }