fork download
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7.  
  8. namespace CSharpConsoleApp
  9. {
  10. class First
  11. {
  12. public string FirstName { get; set; }
  13. public int Age { get; set; }
  14. }
  15.  
  16. class Second
  17. {
  18. public string SecondName { get; set; }
  19. public First FirstObject { get; set; }
  20. }
  21.  
  22. class Program
  23. {
  24. static void ReadProperties(object obj, string prefix = " ")
  25. {
  26. foreach (var prop in obj.GetType().GetProperties())
  27. {
  28. if (prop.GetIndexParameters().Length == 0)
  29. {
  30. Console.WriteLine(prefix + prop.Name + ": " + prop.GetValue(obj, null));
  31. if (!prop.PropertyType.IsPrimitive)
  32. {
  33. ReadProperties(prop.GetValue(obj, null), prefix + " ");
  34. }
  35. }
  36. }
  37. }
  38.  
  39. static void Main(string[] args)
  40. {
  41. var sec = new Second
  42. {
  43. SecondName = "SecondName",
  44. FirstObject = new First { FirstName = "FirstName", Age = 12 }
  45. };
  46.  
  47. ReadProperties(sec);
  48. }
  49. }
  50. }
  51.  
Success #stdin #stdout 0.01s 131520KB
stdin
Standard input is empty
stdout
 SecondName: SecondName
  Length: 10
 FirstObject: CSharpConsoleApp.First
  FirstName: FirstName
   Length: 9
  Age: 12