Ejemplo n.º 1
0
        /// <summary>
        /// Sets the value of this setting.
        /// </summary>
        /// <param name="value">The new value of this setting. If the type of this object is a type that implements <see cref="T:IStringSettingConvertible"/>,
        /// an automatic conversion as defined in that type will be performed.</param>
        /// <param name="setIsModified">Whether or not to set the "IsModified" property if the value is modified.</param>
        internal void SetValue(object value, bool setIsModified)
        {
            // Only set the value if it the types match
            if (value != null && value.GetType().IsSubclassOf(SettingType))
            {
                return;
            }
            if (value == this.Value)
            {
                return;
            }

            object valueToSave = value;

            // If the value implements the IStringSettingConvertible-interface, we can do custom conversion into the string type.
            IStringSettingConvertible convertible = value as IStringSettingConvertible;

            if (convertible != null)
            {
                valueToSave = convertible.ConvertBack();
            }

            this.Value = valueToSave;

            if (setIsModified)
            {
                IsModified = true;
            }
        }
        /// <summary>
        /// Tries to cast the value to <see cref="IStringSettingConvertible"/> and performs the ConvertBack()-method if successful.
        /// Otherwise returns false and does nothing.
        /// </summary>
        /// <param name="value">The value to convert. May be of type <see cref="IStringSettingConvertible"/>.</param>
        /// <param name="converted">If conversion succeeded, this parameter contains the converted value. Otherwise contains null.</param>
        /// <returns>A boolean value indicating whether or not conversion was successful.</returns>
        public static bool ConvertBack(object value, out string converted)
        {
            IStringSettingConvertible convertible = value as IStringSettingConvertible;

            if (convertible != null)
            {
                converted = convertible.ConvertBack();
                return(true);
            }

            converted = null;
            return(false);
        }