} // FormClosing // ---------------------------------------------------------------------- private PropertySetting CreateSetting(string name, string propertyName) { PropertySetting propertySetting = new PropertySetting(name, form, propertyName); propertySetting.ValueSaving += ValueSaving; return(propertySetting); } // CreateSetting
} // Add // ---------------------------------------------------------------------- public void AddAll(object obj) { if (obj == null) { throw new ArgumentNullException("obj"); } // field settings FieldInfo[] fieldInfos = obj.GetType().GetFields( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (FieldInfo fieldInfo in fieldInfos) { FieldSettingAttribute[] settingAttributes = (FieldSettingAttribute[])fieldInfo.GetCustomAttributes(typeof(FieldSettingAttribute), true); if (settingAttributes.Length <= 0) { continue; } FieldSettingAttribute settingAttribute = settingAttributes[0]; string settingName = settingAttribute.Name; if (string.IsNullOrEmpty(settingName)) { settingName = fieldInfo.Name; } object defaultValue = settingAttribute.DefaultValue; FieldSetting fieldSetting = new FieldSetting( settingName, obj, fieldInfo.Name, defaultValue); Add(fieldSetting); } // property settings PropertyInfo[] propertyInfos = obj.GetType().GetProperties( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (PropertyInfo propertyInfo in propertyInfos) { PropertySettingAttribute[] settingAttributes = (PropertySettingAttribute[])propertyInfo.GetCustomAttributes(typeof(PropertySettingAttribute), true); if (settingAttributes.Length <= 0) { continue; } PropertySettingAttribute settingAttribute = settingAttributes[0]; string settingName = settingAttribute.Name; if (string.IsNullOrEmpty(settingName)) { settingName = propertyInfo.Name; } object defaultValue = settingAttribute.DefaultValue; PropertySetting propertySetting = new PropertySetting( settingName, obj, propertyInfo.Name, defaultValue); Add(propertySetting); } } // AddAll
} // Collect // ---------------------------------------------------------------------- private void Collect(DependencyObject currentObject, bool recursive) { foreach (object child in LogicalTreeHelper.GetChildren(currentObject)) { DependencyObject dependencyObject = child as DependencyObject; if (dependencyObject == null) { continue; } FrameworkElement frameworkElement = child as FrameworkElement; if (frameworkElement != null) { // ReSharper disable UseMethodIsInstanceOfType bool add = elementType.IsAssignableFrom(frameworkElement.GetType()); // ReSharper restore UseMethodIsInstanceOfType if (add && string.IsNullOrEmpty(frameworkElement.Name)) { add = false; Debug.WriteLine("PropertySettingCollector: missing name for framework element " + frameworkElement); } if (add && !OnCollectingSetting(frameworkElement)) { add = false; } if (add) { string settingName = string.Concat(frameworkElement.Name, ".", propertyName); PropertySetting propertySetting = new PropertySetting(settingName, frameworkElement, propertyName); propertySetting.DefaultValue = propertySetting.Value; ApplicationSettings.Settings.Add(propertySetting); } } if (recursive) { Collect(dependencyObject, true); } } } // Collect
} // Collect // ---------------------------------------------------------------------- private void Collect(Control.ControlCollection children, bool recursive) { foreach (Control control in children) { bool add = control.GetType().IsAssignableFrom(elementType); string controlId = null; if (add) { controlId = GetControlId(control); if (string.IsNullOrEmpty(controlId)) { add = false; Debug.WriteLine("PropertySettingCollector: missing id for control " + control); } } if (add && !OnCollectingSetting(control)) { add = false; } if (add) { string settingName = string.Concat(controlId, ".", propertyName); PropertySetting propertySetting = new PropertySetting(settingName, control, propertyName); propertySetting.DefaultValue = propertySetting.Value; ApplicationSettings.Settings.Add(propertySetting); } if (recursive && control.Controls.Count > 0) { Collect(control.Controls, true); } } } // Collect