using System.Collections.Generic;
public static void Main()
var dict = new Dictionary<string,object>();
dict.Add("f_name","steve");
dict.Add("age_in_years","36");
dict.Add("isNot_a-Monster",1);
var human = new Human(dict);
Console.WriteLine(human.Name);
Console.WriteLine(human.Age + 2);
Console.WriteLine(human.LikesDogs ? "this dudes alright" : "probably a serial killer");
public string Name {get;set;}
public int Age {get;set;}
public bool LikesDogs {get;set;}
public Human(Dictionary<string,object> valuesWeGotFromSqlHelpers)
if(HelperMethods.TryParseValueFromDictionary<string>(valuesWeGotFromSqlHelpers,"f_name", true, out string name))
Age = HelperMethods.ParseValueFromDictionary<int>(valuesWeGotFromSqlHelpers,"age_in_years", false);
LikesDogs = HelperMethods.ParseValueFromDictionary<bool>(valuesWeGotFromSqlHelpers,"isNot_a-Monster", false);
public static class HelperMethods
public static T ParseValueFromDictionary<T>(Dictionary<string,object> dictionaryToSearch, string valueToLookFor, bool blowUpOnFailure)
if(dictionaryToSearch.ContainsKey(valueToLookFor))
if(typeof(T) == typeof(string))
return (T) dictionaryToSearch[valueToLookFor];
return (T)Convert.ChangeType(dictionaryToSearch[valueToLookFor], typeof(T));
public static bool TryParseValueFromDictionary<T>(Dictionary<string,object> dictionaryToSearch, string valueToLookFor, bool blowUpOnFailure, out T returnValue)
if(dictionaryToSearch.ContainsKey(valueToLookFor))
if(typeof(T) == typeof(string))
returnValue = (T)dictionaryToSearch[valueToLookFor];
returnValue = (T)Convert.ChangeType(dictionaryToSearch[valueToLookFor], typeof(T));
returnValue = default(T);