Get IDictionary from anonymous type


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public static IDictionary<string, object> ToDictionary(this object data)
  2. {
  3. BindingFlags publicAttributes = BindingFlags.Public | BindingFlags.Instance;
  4. Dictionary<string, object> dictionary = new Dictionary<string, object>();
  5.  
  6. foreach (PropertyInfo property in data.GetType().GetProperties(publicAttributes))
  7. {
  8. if (property.CanRead)
  9. dictionary.Add(property.Name, property.GetValue(data, null));
  10. }
  11. return dictionary;
  12. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.