Created
March 2, 2018 14:14
-
-
Save lbargaoanu/86675a5c9e79ae06860d882320efb94f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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