IDictionary<string, object> to ExpandoObject extension method

Yan Cui

I help clients go faster for less using serverless technologies.

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 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


10 thoughts on “IDictionary<string, object> to ExpandoObject extension method”

  1. Pingback: Getting dynamic ExpandoObject to serialize to JSON as expected | PatridgeDev

  2. Pingback: IDictionary to ExpandoObject extension method | theburningmonk.com - wxy

  3. “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?

Leave a Comment

Your email address will not be published. Required fields are marked *