WebConfigurationManager tipado e com valor padrão

int operatorId = WebConfigurationManager.AppSettings.Get<int>("OperatorId");

int operatorIdComDefault = WebConfigurationManager.AppSettings.Get<int>("OperatorId", 1);

public static class NameValueCollectionExtension
{
    public static T Get<T>(this NameValueCollection collection, string key, T defaultValue = default(T)) where T : IConvertible
    {
        var value = collection.Get(key);
        if (value != null)
        {
            return (T)Convert.ChangeType(value, typeof(T));
        }
        return defaultValue;
    }
}
Anúncios