Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Configuration;
- namespace Successful.Misc
- {
- public static class AppSettingsHelper
- {
- private static bool ThrowException { get; set; }
- private static object DefaultValue { get; set; }
- public static TResult Get<TResult>(string key, Func<TResult> defaultValue = null, bool throwException = false)
- {
- ThrowException = throwException;
- DefaultValue = defaultValue;
- var result = default(TResult);
- var valueOfKey = ConfigurationManager.AppSettings[key];
- if (string.IsNullOrWhiteSpace(valueOfKey))
- {
- if (ThrowException && DefaultValue == null)
- {
- throw new ArgumentNullException(valueOfKey,
- $"Failed to get a value for the key {key} in the App.config file.");
- }
- result = GetDefaultValue(defaultValue);
- }
- else
- {
- result = ConvertToResult<TResult>(key, valueOfKey);
- }
- return result;
- }
- private static TResult ConvertToResult<TResult>(string key, string valueOfKey)
- {
- TResult result;
- try
- {
- result = (TResult)Convert.ChangeType(valueOfKey, typeof(TResult));
- }
- catch (Exception ex)
- {
- if (ThrowException && DefaultValue == null)
- {
- throw new Exception($"Failed to convert {valueOfKey} from type {valueOfKey.GetType()} to the type {typeof (TResult)}", ex);
- }
- result = GetDefaultValue((Func<TResult>)Convert.ChangeType(DefaultValue, typeof(Func<TResult>)));
- }
- return result;
- }
- private static TResult GetDefaultValue<TResult>(Func<TResult> defaultValue)
- {
- var result = default(TResult);
- if (defaultValue != null)
- {
- try
- {
- result = defaultValue.Invoke();
- }
- catch (Exception ex)
- {
- if (ThrowException)
- {
- throw new Exception($"Failed to to invoke delegate {defaultValue} of type {defaultValue.GetType()}", ex);
- }
- }
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement