コード例 #1
0
        /// <summary>
        /// Save settings to persistant store
        /// </summary>
        public void Save()
        {
            try
            {
                Type type = typeof(Settings);

                // Get name of the persistant store from the class attribute
                string collectionName = (type.GetCustomAttribute(typeof(SettingsEntity)) as SettingsEntity).Name;

                // Check is the store already exists, otherwise we will create it
                if (!writableSettingsStore.CollectionExists(collectionName))
                {
                    writableSettingsStore.CreateCollection(collectionName);
                }

                // Go through properties. Those marked with SettingsAttribute will be saved
                foreach (PropertyInfo property in type.GetProperties())
                {
                    if (Attribute.IsDefined(property, typeof(SettingsEntity)))
                    {
                        SettingsEntity entity = (SettingsEntity)Attribute.GetCustomAttribute(property, typeof(SettingsEntity));

                        writableSettingsStore.SetString(collectionName, entity.Name, property.GetValue(this).ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Fail(ex.Message);
            }
        }
コード例 #2
0
        /// <summary>
        /// Load settings from store. Generally there is no need to call
        /// this method. Settings are autoloaded in GetSettings method
        /// </summary>
        public void Load()
        {
            try
            {
                Type type = typeof(Settings);

                // Get name of the persistant store from the class attribute
                string collectionName = (type.GetCustomAttribute(typeof(SettingsEntity)) as SettingsEntity).Name;

                // Go through properties. Those marked with SettingsAttribute will be loaded
                foreach (PropertyInfo property in type.GetProperties())
                {
                    if (Attribute.IsDefined(property, typeof(SettingsEntity)))
                    {
                        SettingsEntity entity = (SettingsEntity)Attribute.GetCustomAttribute(property, typeof(SettingsEntity));

                        string val = writableSettingsStore.GetString(collectionName, entity.Name, property.GetValue(this).ToString());
                        object o   = Convert.ChangeType(val, property.PropertyType);
                        property.SetValue(this, o);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Fail(ex.Message);
            }
        }