code_junkie

How do you get the all properties of a class and its base classes (up the hierarchy) with Reflection? (C#)

Nov 11th, 2011
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. PropertyInfo[] info = obj.GetType().GetProperties(BindingFlags.Public);
  2.  
  3. PropertyInfo[] info = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
  4.  
  5. class B
  6. {
  7. public int MyProperty { get; set; }
  8. }
  9.  
  10. class C : B
  11. {
  12. public string MyProperty2 { get; set; }
  13. }
  14.  
  15. static void Main(string[] args)
  16. {
  17. PropertyInfo[] info = new C().GetType().GetProperties();
  18. foreach (var pi in info)
  19. {
  20. Console.WriteLine(pi.Name);
  21. }
  22. }
  23.  
  24. Type.BaseType
  25.  
  26. Type type = obj.GetType();
  27. PropertyInfo[] info = type.GetProperties(BindingFlags.Public);
  28. PropertyInfo[] baseProps = type.BaseType.GetProperties(BindingFlags.Public);
  29.  
  30. TypeDescriptor.GetProperties(obj);
  31.  
  32. foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(obj))
  33. {
  34. Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(obj));
  35. }
Add Comment
Please, Sign In to add comment