Posted By

nivlam on 02/11/11


Tagged


Versions (?)

Get IDictionary from anonymous type


 / Published in: C#
 

  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  

You need to login to post a comment.