Created
June 4, 2012 13:18
-
-
Save Dynyx/2868317 to your computer and use it in GitHub Desktop.
Create an IDictionary for an anonymous type
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 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