Skip to content

Instantly share code, notes, and snippets.

@lbargaoanu
Created March 2, 2018 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbargaoanu/86675a5c9e79ae06860d882320efb94f to your computer and use it in GitHub Desktop.
Save lbargaoanu/86675a5c9e79ae06860d882320efb94f to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Globalization;
using System.Text;
namespace UiPath.Shared
{
public static class AppSettings
{
public static string Get(string key)
{
var value = ConfigurationManager.AppSettings[key];
if(value.IsNullOrEmpty())
{
throw new ConfigurationErrorsException("Missing key in application settings: " + key);
}
return value;
}
public static T Get<T>(string key, T defaultValue)
{
var value = ConfigurationManager.AppSettings[key];
return value.IsNullOrEmpty() ? defaultValue : Parse<T>(value);
}
public static T Get<T>(string key) =>
Parse<T>(Get(key));
private static T Parse<T>(string value) =>
(T) TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment