views:

538

answers:

2

Is there a quick way to convert a Generic Dictionary from one type to another

I have this

IDictionary<string, string> _commands;

and need to pass it to a function that takes a slightly different typed Dictionary

public void Handle(IDictionary<string, Object> _commands);
A: 

maybe this function can be useful for you

IEnumerable<KeyValuePair<string, object>> Convert(IDictionary<string, string> dic) {
    foreach(var item in dic) {
     yield return new KeyValuePair<string, object>(item.Key, item.Value);
    }
}

And you will call it like so:

Handle(Convert(_commands));
Jhonny D. Cano -Leftware-
+5  A: 

I suppose I would write

Handle(_commands.ToDictionary(p => p.Key, p => (object)p.Value));

Not the most efficient thing in the world to do, but until covariance is in, that's the breaks.

mquander