Created
December 11, 2012 19:53
-
-
Save micahlmartin/4261551 to your computer and use it in GitHub Desktop.
Convert anonymous object to dictionary
This file contains hidden or 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 IDictionary<string, object> ToDictionary(this object values) | |
{ | |
var dict = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); | |
if (values != null) | |
{ | |
foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(values)) | |
{ | |
object obj = propertyDescriptor.GetValue(values); | |
dict.Add(propertyDescriptor.Name, obj); | |
} | |
} | |
return dict; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment