Skip to content

Instantly share code, notes, and snippets.

@Dynyx
Created June 4, 2012 13:18
Show Gist options
  • Save Dynyx/2868317 to your computer and use it in GitHub Desktop.
Save Dynyx/2868317 to your computer and use it in GitHub Desktop.
Create an IDictionary for an anonymous type
public static IDictionary<string, object> ToDictionary(this object data)
{
BindingFlags publicAttributes = BindingFlags.Public | BindingFlags.Instance;
Dictionary<string, object> dictionary = new Dictionary<string, object>();
foreach (PropertyInfo property in data.GetType().GetProperties(publicAttributes))
{
if (property.CanRead)
dictionary.Add(property.Name, property.GetValue(data, null));
}
return dictionary;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment