MrMistreater

Get IDictionary from anonymous type

May 9th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.45 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment