andrew4582

TypeExtentions

Oct 6th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Linq;
  4.  
  5. namespace System
  6. {
  7.     public static class TypeExtentions
  8.     {
  9.         public static string[] GetPropertyNames(this object obj)
  10.         {
  11.             return GetPropertyNames(obj.GetType());
  12.         }
  13.  
  14.         public static string[] GetPropertyNames(this Type t)
  15.         {
  16.             return TypeDescriptor.GetProperties(t)
  17.                 .OfType<PropertyDescriptor>()
  18.                 .Select(p => p.Name)
  19.                 .ToArray();
  20.         }
  21.        
  22.         public static Dictionary<string, object> GetPropertyValues(this object obj)
  23.         {
  24.             var dictionary = TypeDescriptor.GetProperties(obj)
  25.               .OfType<PropertyDescriptor>()
  26.               .ToDictionary(p => p.Name, p => p.GetValue(obj));
  27.             return dictionary;
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment