Skip to content

Instantly share code, notes, and snippets.

@droyad
Last active August 29, 2015 14:01
Show Gist options
  • Save droyad/809e97e5c969f4c4e851 to your computer and use it in GitHub Desktop.
Save droyad/809e97e5c969f4c4e851 to your computer and use it in GitHub Desktop.
class ObjectSettingsReader : ISettingsReader
{
private readonly Dictionary<string, string> _values;
public ObjectSettingsReader(object obj)
{
var q = from p in obj.GetType().GetProperties()
where p.CanRead
let v = "" + p.GetValue(obj)
where v != ""
select new { Key = p.Name, Value = v };
_values = q.ToDictionary(x => x.Key, x => x.Value);
}
public string ReadValue(string key)
{
string value;
return _values.TryGetValue(key, out value) ? value : null;
}
public IEnumerable<string> AllKeys
{
get { return _values.Keys; }
}
}
class MultipleSourceSettingsReader : ISettingsReader
{
private readonly ISettingsReader[] _readers;
public MultipleSourceSettingsReader(params ISettingsReader[] readers)
{
}
public string ReadValue(string key)
{
return _readers.Select(r => r.ReadValue(key)).FirstOrDefault (v => v != null);
}
public IEnumerable<string> AllKeys
{
get { return _readers.SelectMany(r => r.AllKeys); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment