Created
August 25, 2014 09:07
-
-
Save sebnilsson/1a3c5e0cd465b2a19775 to your computer and use it in GitHub Desktop.
Extension-method to convert any object to dynamic ExpandoObject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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