Пример #1
0
        /// <summary>
        /// 設定情報(<see cref="Properties.Settings"/>)から、
        /// 引数のプロパティの名称(<paramref name="propertyName"/>)に該当する
        /// プロパティのデフォルト値を取得する
        /// </summary>
        /// <param name="propertyName">プロパティの名称</param>
        /// <exception cref="ArgumentNullException">
        /// 引数のプロパティの名称(<paramref name="propertyName"/>)がNULLの場合に発生
        /// </exception>
        /// <exception cref="SettingException">
        /// 引数で指定された名称のプロパティが設定情報(<see cref="Properties.Settings"/>)に存在しない、
        /// または デフォルト値属性が存在しない場合に発生
        /// </exception>
        /// <returns>
        /// 引数のプロパティの名称(<paramref name="propertyName"/>)に該当する
        /// 設定情報(<see cref="Properties.Settings"/>)で定義されたプロパティのデフォルト値
        /// </returns>
        private static string GetSettingPropertyDefaultValue(string propertyName)
        {
            // NULLチェック
            if (propertyName == null)
            {
                throw new ArgumentNullException(nameof(propertyName));
            }

            // 対象のプロパティの属性情報を取得する
            AttributeCollection attributes = GetSettingProperty(propertyName).Attributes;

            // 取得した属性除法情報からデフォルト値属性を取得
            DefaultSettingValueAttribute defaultValueAttribute
                = attributes == null
                ? null
                : attributes[typeof(DefaultSettingValueAttribute)] as DefaultSettingValueAttribute;

            if (defaultValueAttribute == null)
            {
                throw new SettingException(string.Format(
                                               CultureInfo.InvariantCulture,
                                               Properties.Resources.SettingErrorMessagePropertyNotHaveDefaultAttribute,
                                               propertyName));
            }

            // デフォルト値から値を取得して返却
            return(defaultValueAttribute.Value?.ToString(CultureInfo.InvariantCulture));
        }
        /// @brief Reset the property to its default value.
        /// @param sender Sender object to this event.
        /// @param e Arguments to this event.
        private void ResetButton_Click(object sender, EventArgs e)
        {
            PropertyDescriptor prop;    //to get the underlying property

            //check if a property is selected and if it is writeable
            if (GearPropertyGrid.SelectedGridItem.GridItemType == GridItemType.Property &&
                !(prop = GearPropertyGrid.SelectedGridItem.PropertyDescriptor).IsReadOnly)
            {
                //try to get the default value of the property
                DefaultSettingValueAttribute attr =
                    prop.Attributes[typeof(DefaultSettingValueAttribute)]
                    as DefaultSettingValueAttribute;
                if (attr != null)  //if exist
                {
                    //remember old value
                    object oldValue = prop.GetValue(Settings.Default);
                    //set the new value
                    if (prop.CanResetValue(Settings.Default))
                    {
                        prop.ResetValue(Settings.Default);
                    }
                    else
                    {
                        prop.SetValue(
                            Settings.Default,
                            Convert.ChangeType(attr.Value, prop.PropertyType));
                    }
                    //call the notification event
                    GearPropertyGrid_PropertyValueChanged(sender, new PropertyValueChangedEventArgs(
                                                              GearPropertyGrid.SelectedGridItem, oldValue));
                }
                GearPropertyGrid.Refresh();
            }
        }
Пример #3
0
        static AppSetting()
        {
            /// fetch public property values using Reflection
            foreach (PropertyInfo p in typeof(AppSetting).GetProperties())
            {
                string pt = p.PropertyType.Name;
                string pn = p.Name;

                object o = ReadObject(pn, pt);

                if (o != null && !string.IsNullOrEmpty(pn))
                {
                    p.SetValue(null, o, null);
                }
                else
                {
                    if (p.GetCustomAttributes(true).Length > 0)
                    {
                        object[] defaultValueAttribute =
                            p.GetCustomAttributes(typeof(DefaultSettingValueAttribute), true);
                        if (defaultValueAttribute != null && defaultValueAttribute.Length > 0)
                        {
                            DefaultSettingValueAttribute dva =
                                defaultValueAttribute[0] as DefaultSettingValueAttribute;
                            if (dva != null)
                            {
                                p.SetValue(null, dva.Value, null);
                                o = dva.Value;
                            }
                        }
                    }
                }
                //Debug.WriteLine("public static {0} {1}={2}", pt, pn, o);
            }
            /// fetch fields values using Reflection
            foreach (FieldInfo fi in
                     typeof(AppSetting).GetFields(
                         BindingFlags.NonPublic
                         | BindingFlags.Static))
            {
                string pt  = fi.FieldType.Name;
                string fin = fi.Name;
                if (!fin.StartsWith("<"))
                {
                    object o = ReadObject(fin, pt);
                    fi.SetValue(null, o);

                    /// Debug.WriteLine("private static {0} {1}={2}", pt, fin, fi.GetValue(null));
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Obtains the default value of a setting from the <see cref="DefaultSettingValueAttribute"/>.
        /// </summary>
        /// <remarks>
        /// If translate is true, and the value is the name of an embedded resource, it is automatically translated.
        /// </remarks>
        public static string GetDefaultValue(PropertyInfo property, bool translate)
        {
            DefaultSettingValueAttribute a = CollectionUtils.FirstElement <DefaultSettingValueAttribute>(
                property.GetCustomAttributes(typeof(DefaultSettingValueAttribute), false));

            if (a == null)
            {
                return("");
            }

            if (!translate)
            {
                return(a.Value);
            }

            return(TranslateDefaultValue(property.ReflectedType, a.Value));
        }
Пример #5
0
        /// <summary>
        /// Adds default properties to the settings storage of <paramref name="settingsStorageType"/> type.
        /// Properties are taken from the type definition. Only those are taken which are decorated with
        /// <see cref="DefaultSettingValueAttribute"/> attribute (i.e. have default values)
        /// </summary>
        /// <param name="settingsStorageType">Type of the settings storage to take properties from</param>
        /// <param name="propertyCollection">Collection to add instances of <see cref="SettingsProperty"/> to</param>
        /// <param name="propertyValueCollection">Collection to add corresponding instances of <see cref="SettingsPropertyValue"/> to</param>
        public static void AddDefaultProperties(
            Type settingsStorageType,
            SettingsPropertyCollection propertyCollection,
            SettingsPropertyValueCollection propertyValueCollection)
        {
            IEnumerable <PropertyInfo> settingProperties = GetPropertiesWithAttribute(settingsStorageType, typeof(DefaultSettingValueAttribute));

            foreach (PropertyInfo settingProperty in settingProperties)
            {
                DefaultSettingValueAttribute defaultValueAttribute = settingProperty.GetCustomAttributes(typeof(DefaultSettingValueAttribute), false)[0]
                                                                     as DefaultSettingValueAttribute;
                if (defaultValueAttribute != null)
                {
                    SettingsProperty setting = BuildSettingsProperty(
                        settingProperty.Name, defaultValueAttribute.Value, settingProperty.PropertyType, settingProperty.GetCustomAttributes(true));
                    SettingsPropertyValue settingValue = BuildSettingsPropertyValue(setting);

                    propertyCollection.Add(setting);
                    propertyValueCollection.Add(settingValue);
                }
            }
        }