How can I convert a Dictionary(Of TypeA, TypeB) to a Dictionary(Of TypeC, TypeD)? There is an implicit cast when converting (let’s say the second dictionary is String, String), so this process should be automatic, right? I just can’t figure out an elegant way to do this.
The language is VB.NET, but I can read and convert C# if you’re more comfortable with that.
I would use Linq for cases like this. There’s a
ToDictionary()method that converts any generic IEnumerable (and aDictionary<TA,TB>is anIEnumerable<KeyValuePair<TA,TB>>) to a Dictionary by taking two projections of the Enumerable element to produce a key and value for the dictionary. It makes these kinds of conversions very simple:AsTypeC() and AsTypeD() are placeholders for whatever conversion is necessary to go from A/B to C/D. You suggested the second Dictionary was a
Dictionary<string,string>; in that case you’d simply ToString() both of them (if you’ve overridden that method) or use the Key/Value in some String.Format operation.