containers - C# Casting of Dictionary -
i have function:
void writemap(object value) { idictionary<string, object> vv = (idictionary<string, object>)value; foreach (keyvaluepair<string, object> obj in vv) { (write key) (write value) } }
it works if value of type idictionary<string, object>
, like:
idictionary<string, int>
idictionary<string, myclass>
idictionary<string, string[]>
etc.
the cast throws run-time exception. function not modify container, prints keys , values. ideas on how can make work? appreciated.
thanks!
add generic type parameter method corresponding type of values in dictionary:
void writemap<t>(idictionary<string, t> dict) { foreach (keyvaluepair<string, t> in dict) { // write key // write value } }
this has advantage of eliminating cast. if need pass dictionary in object reason, cast idictionary
.
Comments
Post a Comment