Skip to content

Instantly share code, notes, and snippets.

@micahlmartin
Created December 11, 2012 19:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save micahlmartin/4261551 to your computer and use it in GitHub Desktop.
Save micahlmartin/4261551 to your computer and use it in GitHub Desktop.
Convert anonymous object to dictionary
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