2

OK, there is just something like this:

Dictionary<string, dynamic> object

And I have a Dictionary<string, string> object like this :

{"great":"well","well":"WTF?",...}

But Dictionary<string, dynamic> dyndict = new Dictionary<string, string>() is actually not available.

Is there some good way to assign the latter dict to the dynamic one?

Maybe I'll use a while loop? But what if have some variant Dictionary<string, int>orDictionary<string, MyObject> or so on? Is there some method like extend in C#?

How can I be more elegant? (And that's why I choose C#! :) )

UPDATE1

What I want to figure out is a generic way to extend my dynamic-value dictionary. Can Extension Methods help me out? Or am I wrong at the start point?

UPDATE2

To be more specific, what I what is to achieve an auto duck-type dict......The ToDictionary() is also typed dict, I want to know wether there is a way like this:

Dictionary<string, dynamic> dyndict = new Dictionary<string, dynamic>()

Dictionary<string, string> otherDict = new Dictionary<string, string>(){...}

dyndict.extend(otherDict )

ToDictionary(),right....Thanks!

Community
  • 1
  • 1
Lulus Young
  • 43
  • 1
  • 7
  • Let me get this straight. You want to assign the values from `Dictionary` to `Dictionary` ? – Bauss Aug 24 '15 at 09:11
  • I'm not sure I understood your problem.. do you want to convert a `Dictionary` to `Dictionary`? – dcastro Aug 24 '15 at 09:11
  • 1
    You need to create a dictionary of the right type, and then copy over all the keys and values, the two types are not assignment compatible. – Lasse V. Karlsen Aug 24 '15 at 09:16
  • a `Dictionary` is more or less equivalent to `Dictionary` at runtime. – Amit Kumar Ghosh Aug 24 '15 at 09:17
  • Nope, I know the assignment is not right, but really, I'm trying to figure out a good way to avoid using handwriting loop to assign a specific typed dictionary to a dictionary with dynamic value. @dcastro@Bauss – Lulus Young Aug 24 '15 at 09:18
  • See the duplicate question, use `ToDictionary` to cast the values to `dynamic`. – dcastro Aug 24 '15 at 09:20
  • @AmitKumarGhosh, that perfectly true, but actually you don't know whether it provided a **struct** or simple-typed value in the dictionary. – Lulus Young Aug 24 '15 at 09:21

0 Answers0