fork download
  1. using System;
  2.  
  3. public class A
  4. {
  5. public B MyProperty { get;set; }
  6. }
  7.  
  8. public class B
  9. {
  10. public string Text { get;set; }
  11. }
  12.  
  13. static class PropertyEval
  14. {
  15. public static string GetValueFromProperty(this object obj, string Name)
  16. {
  17. var methods = Name.Split(new [] {"."}, StringSplitOptions.RemoveEmptyEntries);
  18.  
  19. object current = obj;
  20. object result = null;
  21. foreach(var method in methods)
  22. {
  23. var prop = current.GetType().GetProperty(method);
  24. result = prop != null ? prop.GetValue(current, null) : null;
  25. current = result;
  26. }
  27. return result.ToString();
  28. }
  29. }
  30.  
  31. public class Test
  32. {
  33.  
  34. public static void Main()
  35. {
  36. A a = new A();
  37. a.MyProperty = new B() { Text = "Success" };
  38.  
  39. Console.WriteLine(PropertyEval.GetValueFromProperty(a, "MyProperty.Text"));
  40. }
  41. }
Success #stdin #stdout 0.05s 34160KB
stdin
Standard input is empty
stdout
Success