Beispiel #1
0
        } // 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
Beispiel #2
0
        } // 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)
                {
                    bool add = elementType.IsAssignableFrom(frameworkElement.GetType());

                    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(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)
                {
                    bool add = elementType.IsAssignableFrom(frameworkElement.GetType());

                    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
        } // 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