Example #1
0
        /// <summary>
        /// Gets a value with a default value...
        /// </summary>
        /// <param name="readValue">
        /// </param>
        /// <param name="key">
        /// </param>
        /// <param name="defaultValue"></param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// </returns>
        public static T Get <T>([NotNull] this IReadValue <T> readValue, [NotNull] string key, [CanBeNull] T defaultValue)
        {
            CodeContracts.VerifyNotNull(readValue, "readValue");
            CodeContracts.VerifyNotNull(key, "key");

            var value = readValue.Get(key);

            return(Equals(value, default(T)) ? defaultValue : value);
        }
Example #2
0
        /// <summary>
        /// The get as bool.
        /// </summary>
        /// <param name="readValue">
        /// The read value.
        /// </param>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <param name="defaultValue">
        /// The default value.
        /// </param>
        /// <returns>
        /// The get as bool.
        /// </returns>
        public static bool GetAsBool([NotNull] this IReadValue <string> readValue, [NotNull] string key, bool defaultValue)
        {
            CodeContracts.VerifyNotNull(readValue, "readValue");
            CodeContracts.VerifyNotNull(key, "key");

            var value = readValue.Get(key);

            return(Equals(value, null) ? defaultValue : Convert.ToBoolean(value.ToLower()));
        }
Example #3
0
    /// <summary>
    /// Gets a value with a default value...
    /// </summary>
    /// <param name="readValue">
    /// </param>
    /// <param name="key">
    /// </param>
    /// <param name="getValue">
    /// </param>
    /// <typeparam name="T">
    /// </typeparam>
    /// <returns>
    /// </returns>
    public static T Get <T>([NotNull] this IReadValue <T> readValue, [NotNull] string key, [NotNull] Func <T> getValue)
    {
        CodeContracts.VerifyNotNull(readValue);
        CodeContracts.VerifyNotNull(key);
        CodeContracts.VerifyNotNull(getValue);

        var value = readValue.Get(key);

        return(Equals(value, default(T)) ? getValue() : value);
    }
Example #4
0
        public static T Get <T>(
            [NotNull] this IReadValue <string> readValue,
            [NotNull] string key,
            [CanBeNull] T defaultValue = default(T))
        {
            if (readValue == null)
            {
                throw new ArgumentNullException(nameof(readValue));
            }
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            return(readValue.Get(key, () => defaultValue));
        }
Example #5
0
        public static T Get <T>(
            [NotNull] this IReadValue <string> readValue,
            [NotNull] string key,
            [CanBeNull] T defaultValue = default(T))
        {
            if (readValue == null)
            {
                throw new ArgumentNullException(nameof(readValue));
            }
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            string value = readValue.Get(key);

            return(value.IsDefault() ? defaultValue : value.ToType <T>());
        }
Example #6
0
        public static T Get <T>(
            [NotNull] this IReadValue <string> readValue,
            [NotNull] string key,
            [NotNull] Func <T> getDefaultValue)
        {
            if (readValue == null)
            {
                throw new ArgumentNullException(nameof(readValue));
            }
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (getDefaultValue == null)
            {
                throw new ArgumentNullException(nameof(getDefaultValue));
            }

            string value = readValue.Get(key);

            return(value.IsNullOrWhiteSpace() ? getDefaultValue() : value.ToType <T>());
        }