Yan Cui
I help clients go faster for less using serverless technologies.
This article is brought to you by
MongoDB 8.0 is here to change the game. Faster reads and inserts, and brand-new vector search to support modern AI-powered apps.
As you know, the ExpandoObject class implements the IDictionary<string, object> interface, so if you have an ExpandoObject you can easily cast it to an IDictionary<string, object> but there’s no built-in way to easily do the reverse.
Luckily, I came across a very useful extension method today which converts an IDictionary<string, object> into an ExpandoObject, which you can then use dynamically in your code, sweet! :-)
With some small modifications, here’s the code I ended up with, with some comments thrown in for good measures:
1: /// <summary>
2: /// Extension method that turns a dictionary of string and object to an ExpandoObject
3: /// </summary>
4: public static ExpandoObject ToExpando(this IDictionary<string, object> dictionary)
5: {
6: var expando = new ExpandoObject();
7: var expandoDic = (IDictionary<string, object>)expando;
8:
9: // go through the items in the dictionary and copy over the key value pairs)
10: foreach (var kvp in dictionary)
11: {
12: // if the value can also be turned into an ExpandoObject, then do it!
13: if (kvp.Value is IDictionary<string, object>)
14: {
15: var expandoValue = ((IDictionary<string, object>)kvp.Value).ToExpando();
16: expandoDic.Add(kvp.Key, expandoValue);
17: }
18: else if (kvp.Value is ICollection)
19: {
20: // iterate through the collection and convert any strin-object dictionaries
21: // along the way into expando objects
22: var itemList = new List<object>();
23: foreach (var item in (ICollection)kvp.Value)
24: {
25: if (item is IDictionary<string, object>)
26: {
27: var expandoItem = ((IDictionary<string, object>) item).ToExpando();
28: itemList.Add(expandoItem);
29: }
30: else
31: {
32: itemList.Add(item);
33: }
34: }
35:
36: expandoDic.Add(kvp.Key, itemList);
37: }
38: else
39: {
40: expandoDic.Add(kvp);
41: }
42: }
43:
44: return expando;
45: }
Enjoy!
Whenever you’re ready, here are 3 ways I can help you:
- Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
- I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
- Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.
Pingback: Getting dynamic ExpandoObject to serialize to JSON as expected | PatridgeDev
Thank you, very useful!!
Simply AMAZING!!!!
Pingback: IDictionary to ExpandoObject extension method | theburningmonk.com - wxy
You saved me. I clicked on all your ads! Thanks for sharing this useful code.
haha, thanks! glad you find it useful ;-)
couldn’t find ToExpando( ) method in collection class, please help
“ToExpando()” is what we’re defining here, as an extension method to IDictionary.
We also call it recursively in the body of this extension method, so you won’t find it (or need it) on ICollection class, does this make sense?
Thanks
Very nice, man!
A million thanks!