Example #1
0
        /// <summary>
        /// Save an object to ini file, except for the properties marked with the DontSerializeAttribute.
        /// </summary>
        /// <param name="o">object to save</param>
        /// <param name="section">[Section] to save to</param>
        /// <param name="file">ini file name</param>
        /// <param name="makebackupfile">makes a backup file</param>
        public static void _Save(object o, string section, string file)
        {
            lock (IniSerializer.sm_locker)
            {
                //get a temp file
                string tempfile = file;

                if (o == null)
                {
                    return;
                }
                Type           t   = o.GetType();
                PropertyInfo[] pis = t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.SetField);//

                foreach (PropertyInfo pi in pis)
                {
                    try
                    {
                        DontSerializeAttribute[] fields = (DontSerializeAttribute[])pi.GetCustomAttributes(typeof(DontSerializeAttribute), true);
                        if (fields.Length == 0)
                        {
                            bool   hassetandget = pi.CanRead & pi.CanWrite;
                            object v            = pi.GetValue(o, null);
                            Type   tval         = v.GetType();
                            string key          = pi.Name;
                            {
                                if ((v.GetType().IsPrimitive) || (v.GetType().IsEnum))
                                {
                                    if (hassetandget)
                                    {
                                        string inival = StringConverter.ConvertToString(v);
                                        string defval = "";
                                        DefaultValueAttribute[] defvalfields = (DefaultValueAttribute[])pi.GetCustomAttributes(typeof(DefaultValueAttribute), true);
                                        if (defvalfields.Length > 0)
                                        {
                                            if (defvalfields[0].Value != null)
                                            {
                                                defval = StringConverter.ConvertToString(defvalfields[0].Value);
                                            }
                                        }
                                        string curinival = Ini.sGetPrivateProfileString(section, key, tempfile, defval);
                                        if ((curinival != inival) || SaveDefaultValues)
                                        {
                                            Ini.WritePrivateProfileString(section, key, inival, tempfile);
                                        }
                                    }
                                }
                                else
                                {
                                    _Save(v, section + "." + key, file);
                                }
                            }
                        }
                    }
                    catch (Exception exc) { MsgBox.ShowException(exc); }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Load an object from ini file, except for the properties marked with the DontSerializeAttribute.
        /// </summary>
        /// <param name="o">object to save</param>
        /// <param name="section">[Section] to save to</param>
        /// <param name="file">ini file name</param>
        /// <param name="makebackupfile">makes a backup file</param>
        public static void Load(object o, string section, string file)
        {
            lock (IniSerializer.sm_locker)
            {
                //get a temp file
                string tempfile = file;

                if (o == null)
                {
                    return;
                }
                Type           t   = o.GetType();
                PropertyInfo[] pis = t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.SetField);//

                foreach (PropertyInfo pi in pis)
                {
                    try
                    {
                        DontSerializeAttribute[] fields = (DontSerializeAttribute[])pi.GetCustomAttributes(typeof(DontSerializeAttribute), true);
                        if (fields.Length == 0)
                        {
                            bool   hassetandget = pi.CanRead & pi.CanWrite;
                            object v            = pi.GetValue(o, null);
                            string key          = pi.Name;
                            {
                                if ((v.GetType().IsPrimitive) || (v.GetType().IsEnum))
                                {
                                    if (hassetandget)
                                    {
                                        string inival = v.ToString();
                                        inival = Ini.sGetPrivateProfileString(section, key, file, "");
                                        if (inival.Length > 0)
                                        {
                                            try
                                            {
                                                Type proptype = v.GetType();
                                                v = StringConverter.ConvertFromString(inival, proptype);
                                                if (pi.CanWrite)
                                                {
                                                    pi.SetValue(o, v, null);
                                                }
                                            }
                                            catch { }
                                        }
                                    }
                                }
                                else
                                {
                                    Load(v, section + "." + key, file);
                                }
                            }
                        }
                    }
                    catch (Exception exc) { MsgBox.ShowException(exc); }
                }
            }
        }