using System;
using System.Configuration;
using System.Linq;
namespace RainmeterStudio.Business
{
public static class SettingsProvider
{
///
/// Attempts to retrieve the setting of type T, where T is class
///
/// Any class type
/// Name of setting
/// Retrieved setting, or null if not found
public static T GetSetting (string name) where T : class
{
var property = Properties.Settings.Default.Properties
.OfType()
.FirstOrDefault(x => String.Equals(x.Name, name));
return (property == null) ? null : (property.DefaultValue as T);
}
///
/// Attempts to retrieve the setting of type T
///
/// Any type
/// Name of setting
/// Output value
/// True if attempt was successful
public static bool TryGetSetting(string name, out T value)
{
var property = Properties.Settings.Default.Properties.OfType().FirstOrDefault(x => x.Name.Equals(name));
if (property != null)
{
value = (T)property.DefaultValue;
return true;
}
else
{
value = default(T);
return false;
}
}
///
/// Saves the settings
///
public static void SaveSettings()
{
Properties.Settings.Default.Save();
}
///
/// Resets settings to default
///
public static void ResetSettings()
{
Properties.Settings.Default.Reset();
}
}
}