Пример #1
0
        /// <summary>
        /// Reads the option value
        /// </summary>
        /// <typeparam name="T">Option type</typeparam>
        /// <param name="option">Option reference</param>
        /// <returns>Option value</returns>
        public T Read <T>(Option <T> option)
        {
            CheckConfigured();

            CheckCanParse(option.NonNullableType);

            OptionValue optionValue;

            _nameToOptionValue.TryGetValue(option.Name, out optionValue);

            if (!optionValue.IsExpired(_config.CacheTimeout))
            {
                return((T)optionValue.RawValue);
            }

            string value = ReadFirstValue(option.Name);

            if (value == null)
            {
                optionValue.RawValue = option.DefaultValue;
            }
            else if (DefaultParser.IsSupported(option.NonNullableType))
            {
                object resultObject;
                if (DefaultParser.TryParse(value, option.NonNullableType, out resultObject))
                {
                    optionValue.Update <T>((T)resultObject);
                }
                else
                {
                    optionValue.Update(option.DefaultValue);
                }
            }
            else
            {
                ITypeParser typeParser = _config.GetParser(option.NonNullableType);
                object      result;
                typeParser.TryParse(value, option.NonNullableType, out result);
                optionValue.Update <T>((T)result);
            }

            OnReadOption(option, optionValue.RawValue);

            return((T)optionValue.RawValue);
        }