What about
public static void GetValueAs<T, R>(this IDictionary<string, R> dictionary, string fieldName, out T value) where T : R { value = default(T); dictionary.TryGetValue(fieldName, out value) }
Then you can do something like
List<int> list; dictionary.GetValueAs("fieldName", out list);
Basically, to determine that T you should have something like T in the parameters.
Edit
Perhaps the best way is
public static T GetValueAs<T, R>( this IDictionary<string, R> dictionary, string fieldName, T defaultValue) where T : R { R value = default(R); return dictionary.TryGetValue(fieldName, out value) ? (T)value : defaultValue; }
Then you can use var and chain, and this gives you the ability to control what the default is.
var x = dict.GetValueAs("A", new Dictionary<string,int>).GetValueAs("B", default(int));
source share