Esempio n. 1
0
        /// <summary>
        /// Gets the configuration value.
        /// </summary>
        /// <typeparam name="T">The type of the value to retrieve.</typeparam>
        /// <param name="container">The container.</param>
        /// <param name="key">The key.</param>
        /// <param name="defaultValue">The default value. Will be returned if the value cannot be found.</param>
        /// <returns>The configuration value.</returns>
        /// <exception cref="ArgumentException">The <paramref name="key" /> is <c>null</c> or whitespace.</exception>
        public T GetValue <T>(ConfigurationContainer container, string key, T defaultValue = default(T))
        {
            Argument.IsNotNullOrWhitespace("key", key);

            key = GetFinalKey(key);

            try
            {
                if (!ValueExists(container, key))
                {
                    return(defaultValue);
                }

                var value = GetValueFromStore(container, key);
                if (value == null)
                {
                    return(defaultValue);
                }

                // ObjectConverterService doesn't support object, but just return the value as is
                if (typeof(T) == typeof(object))
                {
                    return((T)(object)value);
                }

                return((T)_objectConverterService.ConvertFromStringToObject(value, typeof(T), CultureInfo.InvariantCulture));
            }
            catch (Exception ex)
            {
                Log.Warning(ex, $"Failed to retrieve configuration value '{container}.{key}', returning default value");

                return(defaultValue);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the configuration value.
        /// </summary>
        /// <typeparam name="T">The type of the value to retrieve.</typeparam>
        /// <param name="key">The key.</param>
        /// <param name="defaultValue">The default value. Will be returned if the value cannot be found.</param>
        /// <returns>The configuration value.</returns>
        /// <exception cref="ArgumentException">The <paramref name="key" /> is <c>null</c> or whitespace.</exception>
        public T GetValue <T>(string key, T defaultValue = default(T))
        {
            Argument.IsNotNullOrWhitespace("key", key);

            key = GetFinalKey(key);

            if (!ValueExists(key))
            {
                return(defaultValue);
            }

            try
            {
                var value = GetValueFromStore(key);
                if (value == null)
                {
                    return(defaultValue);
                }

                return((T)_objectConverterService.ConvertFromStringToObject(value, typeof(T)));
            }
            catch (Exception)
            {
                return(defaultValue);
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Converts the specified string value to an object.
 /// </summary>
 /// <typeparam name="T">The type to convert to.</typeparam>
 /// <param name="service">The service.</param>
 /// <param name="value">The value.</param>
 /// <returns>The object value.</returns>
 public static T ConvertFromStringToObject <T>(this IObjectConverterService service, string value)
 {
     return((T)service.ConvertFromStringToObject(value, typeof(T)));
 }