Esempio n. 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);
        }
Esempio n. 2
0
        public bool TryParse(Type propertyType, string rawValue, out object result)
        {
            if (_defaultParser.IsSupported(propertyType)) //type here must be a non-nullable one
            {
                if (!_defaultParser.TryParse(rawValue, propertyType, out result))
                {
                    return(false);
                }
            }
            else
            {
                ITypeParser typeParser = GetParser(propertyType);
                if (!typeParser.TryParse(rawValue, propertyType, out result))
                {
                    return(false);
                }
            }

            return(true);
        }
                /// <summary>
                ///     Initializes a safe parser of type <typeparamref name="T" />.
                /// </summary>
                /// <returns>
                ///     A delegate that parses the specified string using the
                ///     specified format provider to type <typeparamref name="T" />.
                /// </returns>
                /// <remarks>
                ///     The underlying parser uses the <see cref="DefaultParser{T}.TryParse" />
                ///     when the specified string is not null.
                /// </remarks>
                private static TPF <T?> InitTryParse()
                {
                    return((string s, IFormatProvider provider, out T? result) =>
                    {
                        if (s == null)
                        {
                            result = default;
                            return true;
                        }

                        if (DefaultParser <T> .TryParse(s, provider, out var r))
                        {
                            result = r;
                            return true;
                        }

                        result = default;
                        return false;
                    });
                }