Last active
August 29, 2015 14:01
-
-
Save droyad/809e97e5c969f4c4e851 to your computer and use it in GitHub Desktop.
This file contains 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
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