/// <summary> /// Populates properties in super class from data in the database, and adds new properties /// defined in the super class to the database. /// </summary> private void UpdateAndSyncSettings() { _allSettings = new List <DBSetting>(); foreach (PropertyInfo currProperty in propertyLookup.Values) { try { SyncSetting(currProperty); CornerstoneSettingAttribute attribute = attributeLookup[currProperty]; DBSetting setting = this[attribute.Identifier]; currProperty.GetSetMethod().Invoke(this, new Object[] { setting.Value }); _allSettings.Add(setting); } catch (Exception e) { if (e is ThreadAbortException) { throw e; } logger.ErrorException("Failed loading setting data for " + currProperty.Name + ".", e); } } }
/// <summary> /// Stores property and attribute info from the super class for quick lookup later. /// </summary> private void BuildPropertyLookup() { propertyLookup = new Dictionary <string, PropertyInfo>(); attributeLookup = new Dictionary <PropertyInfo, CornerstoneSettingAttribute>(); foreach (PropertyInfo currProperty in GetType().GetProperties()) { // make sure this property is intended to be a setting object[] attributes = currProperty.GetCustomAttributes(typeof(CornerstoneSettingAttribute), true); if (attributes.Length == 0) { continue; } // and store it's info for quick access later CornerstoneSettingAttribute attribute = attributes[0] as CornerstoneSettingAttribute; propertyLookup[attribute.Identifier] = currProperty; attributeLookup[currProperty] = attribute; } }
private void SyncSetting(PropertyInfo property) { CornerstoneSettingAttribute attribute = attributeLookup[property]; StringList groups = new StringList(attribute.Groups); if (!this.ContainsKey(attribute.Identifier)) { DBSetting newSetting = new DBSetting(); newSetting.Key = attribute.Identifier; newSetting.Name = attribute.Name; newSetting.Value = attribute.Default; newSetting.Type = DBSetting.TypeLookup(property.PropertyType); newSetting.Description = attribute.Description; newSetting.MoreInfoLink = attribute.MoreInfoLink; newSetting.Hidden = attribute.Hidden; newSetting.Sensitive = attribute.Sensitive; newSetting.Grouping.AddRange(groups); newSetting.DBManager = this.dbManager; newSetting.SettingsManager = this; newSetting.Commit(); this[attribute.Identifier] = newSetting; } else { DBSetting existingSetting = this[attribute.Identifier]; // update name if necessary if (!existingSetting.Name.Equals(attribute.Name)) { existingSetting.Name = attribute.Name; } // update description if necessary if (!existingSetting.Description.Equals(attribute.Description)) { existingSetting.Description = attribute.Description; } // update link existingSetting.MoreInfoLink = attribute.MoreInfoLink; existingSetting.Hidden = attribute.Hidden; existingSetting.Sensitive = attribute.Sensitive; // update groups if necessary bool reloadGrouping = false; if (existingSetting.Grouping.Count != groups.Count) { reloadGrouping = true; } else { for (int i = 0; i < existingSetting.Grouping.Count; i++) { if (i >= groups.Count || !existingSetting.Grouping[i].Equals(groups[i])) { reloadGrouping = true; break; } } } if (reloadGrouping) { existingSetting.Grouping.Clear(); existingSetting.Grouping.AddRange(groups); } existingSetting.Commit(); } }