code_junkie

Best way to get sub properties using GetProperty

Nov 12th, 2011
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. public class Address
  2. {
  3. public string ZipCode {get; set;}
  4. }
  5.  
  6. public class Customer
  7. {
  8. public Address Address {get; set;}
  9. }
  10.  
  11. Typeof(Customer).GetProperty("ZipCode")?
  12.  
  13. PropertyInfo addressProperty = typeof(Customer).GetProperty("Address");
  14. ProportyInfo zipCodeProperty = addressProperty.PropertyType.GetProperty("ZipCode");
  15.  
  16. object address = addressProperty.GetProperty(customer, null);
  17. object zipCode = zipCodeProperty.GetProperty(address, null);
  18.  
  19. public static object FollowPropertyPath(object value, string path)
  20. {
  21. Type currentType = value.GetType();
  22.  
  23. foreach (string propertyName in path.Split('.'))
  24. {
  25. PropertyInfo property = currentType.GetProperty(propertyName);
  26. value = property.GetValue(value, null);
  27. currentType = property.PropertyType;
  28. }
  29. return value;
  30. }
  31.  
  32. object zipCode = FollowPropertyPath(customer, "Address.ZipCode");
  33.  
  34. static void Main()
  35. {
  36. object obj = new Customer { Address = new Address { ZipCode = "abcdef" } };
  37.  
  38. object address = GetValue(obj, "Address");
  39. object zip = GetValue(address, "ZipCode");
  40.  
  41. Console.WriteLine(zip);
  42. }
  43. static object GetValue(object component, string propertyName)
  44. {
  45. return TypeDescriptor.GetProperties(component)[propertyName].GetValue(component);
  46. }
  47.  
  48. static object ResolveValue(object component, string path) {
  49. foreach(string segment in path.Split('.')) {
  50. if (component == null) return null;
  51. if(component is IListSource) {
  52. component = ((IListSource)component).GetList();
  53. }
  54. if (component is IList) {
  55. component = ((IList)component)[0];
  56. }
  57. component = GetValue(component, segment);
  58. }
  59. return component;
  60. }
  61.  
  62. public static class ReflectorUtil
  63. {
  64. public static object FollowPropertyPath(object value, string path)
  65. {
  66. if (value == null) throw new ArgumentNullException("value");
  67. if (path == null) throw new ArgumentNullException("path");
  68.  
  69. Type currentType = value.GetType();
  70.  
  71. object obj = value;
  72. foreach (string propertyName in path.Split('.'))
  73. {
  74. if (currentType != null)
  75. {
  76. PropertyInfo property = null;
  77. int brackStart = propertyName.IndexOf("[");
  78. int brackEnd = propertyName.IndexOf("]");
  79.  
  80. property = currentType.GetProperty(brackStart > 0 ? propertyName.Substring(0, brackStart) : propertyName);
  81. obj = property.GetValue(obj, null);
  82.  
  83. if (brackStart > 0)
  84. {
  85. string index = propertyName.Substring(brackStart + 1, brackEnd - brackStart - 1);
  86. foreach (Type iType in obj.GetType().GetInterfaces())
  87. {
  88. if (iType.IsGenericType && iType.GetGenericTypeDefinition() == typeof(IDictionary<,>))
  89. {
  90. obj = typeof(ReflectorUtil).GetMethod("GetDictionaryElement")
  91. .MakeGenericMethod(iType.GetGenericArguments())
  92. .Invoke(null, new object[] { obj, index });
  93. break;
  94. }
  95. if (iType.IsGenericType && iType.GetGenericTypeDefinition() == typeof(IList<>))
  96. {
  97. obj = typeof(ReflectorUtil).GetMethod("GetListElement")
  98. .MakeGenericMethod(iType.GetGenericArguments())
  99. .Invoke(null, new object[] { obj, index });
  100. break;
  101. }
  102. }
  103. }
  104.  
  105. currentType = obj != null ? obj.GetType() : null; //property.PropertyType;
  106. }
  107. else return null;
  108. }
  109. return obj;
  110. }
  111.  
  112. public static TValue GetDictionaryElement<TKey, TValue>(IDictionary<TKey, TValue> dict, object index)
  113. {
  114. TKey key = (TKey)Convert.ChangeType(index, typeof(TKey), null);
  115. return dict[key];
  116. }
  117.  
  118. public static T GetListElement<T>(IList<T> list, object index)
  119. {
  120. return list[Convert.ToInt32(index)];
  121. }
  122.  
  123. }
  124.  
  125. typeof (Customer).GetProperty("Address").PropertyType.GetProperty("ZipCode")
Add Comment
Please, Sign In to add comment