Skip to content

Instantly share code, notes, and snippets.

@sebnilsson
Created August 25, 2014 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebnilsson/1a3c5e0cd465b2a19775 to your computer and use it in GitHub Desktop.
Save sebnilsson/1a3c5e0cd465b2a19775 to your computer and use it in GitHub Desktop.
Extension-method to convert any object to dynamic ExpandoObject
public static ExpandoObject ToDynamic(this object obj)
{
var expandoObject = new ExpandoObject();
if (obj == null)
{
return expandoObject;
}
var dictionaryValues = new RouteValueDictionary(obj);
if (!dictionaryValues.Any())
{
return expandoObject;
}
var expandoDictionary = expandoObject as IDictionary<string, object>;
foreach (var value in dictionaryValues)
{
expandoDictionary.Add(value.Key, value.Value);
}
return expandoObject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment