Пример #1
0
        /// <summary>
        /// Gets the default value for specified option type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="customOption">The custom option.</param>
        /// <returns>
        /// Default types value
        /// </returns>
        private object GetDefaultValueForType(OptionType type, ICustomOption customOption)
        {
            switch (type)
            {
            case OptionType.Text:
            case OptionType.DateTime:
            case OptionType.Integer:
            case OptionType.Float:
            case OptionType.JavaScriptUrl:
            case OptionType.CssUrl:
                return(null);

            case OptionType.Boolean:
                return(false);

            case OptionType.Custom:
                ICustomOptionProvider provider = GetCustomOptionsProvider(customOption);

                if (provider != null)
                {
                    return(provider.GetDefaultValueForType());
                }

                return(null);

            default:
                throw new NotSupportedException(string.Format("Not supported option type: {0}", type));
            }
        }
Пример #2
0
        /// <summary>
        /// Gets the custom options provider.
        /// </summary>
        /// <returns>Custom options provider</returns>
        private ICustomOptionProvider GetCustomOptionsProvider(ICustomOption customOption)
        {
            if (customOption == null)
            {
                return(null);
            }

            return(CustomOptionsProvider.GetProvider(customOption.Identifier));
        }
Пример #3
0
        /// <summary>
        /// Converts option value to the correct value type.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="type">The type.</param>
        /// <param name="customOption">The custom option.</param>
        /// <returns>
        /// Value, converted to correct type
        /// </returns>
        /// <exception cref="System.NotSupportedException"></exception>
        private object ConvertValueToCorrectType(string value, OptionType type, ICustomOption customOption)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(GetDefaultValueForType(type, customOption));
            }

            switch (type)
            {
            case OptionType.Text:
                return(value);

            case OptionType.JavaScriptUrl:
            case OptionType.CssUrl:
                return(HttpUtility.UrlPathEncode(value));

            case OptionType.Integer:
                int castedInt;
                if (Int32.TryParse(value, out castedInt))
                {
                    return(castedInt);
                }
                return(Convert.ToInt64(value, CultureInfo.InvariantCulture));

            case OptionType.Float:
                value = value.Replace(",", ".");
                return(Convert.ToDecimal(value, CultureInfo.InvariantCulture));

            case OptionType.DateTime:
                return(DateTime.ParseExact(value, "yyyy-MM-dd", null));    // ISO 8601

            case OptionType.Boolean:
                return(Convert.ToBoolean(value));

            case OptionType.Custom:
                ICustomOptionProvider provider = GetCustomOptionsProvider(customOption);

                if (provider != null)
                {
                    return(provider.ConvertValueToCorrectType(value));
                }

                return(value);

            default:
                throw new NotSupportedException(string.Format("Not supported option type: {0}", type));
            }
        }