Is it possible to cast a Dictionary
to a consistent intermediate generic type? So I would be able to cast
Consider whether casting to and from object
really is necessary. I started down that path and stumbled across this article, before realising I could achieve what I needed through generics rather than conversion. For example;
class DictionaryUtils
{
public static T ValueOrDefault(IDictionary dictionary, string key)
{
return dictionary.ContainsKey(key) ? dictionary[key] : default(T);
}
}
This bit of code is much cleaner and will be faster than the equivalent conversion code shown in other answers.