ReflectionHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace Abp.Reflection
  6. {
  7. /// <summary>
  8. /// Defines helper methods for reflection.
  9. /// </summary>
  10. internal static class ReflectionHelper
  11. {
  12. /// <summary>
  13. /// Checks whether <paramref name="givenType"/> implements/inherits <paramref name="genericType"/>.
  14. /// </summary>
  15. /// <param name="givenType">Type to check</param>
  16. /// <param name="genericType">Generic type</param>
  17. public static bool IsAssignableToGenericType(Type givenType, Type genericType)
  18. {
  19. var givenTypeInfo = givenType.GetTypeInfo();
  20. if (givenTypeInfo.IsGenericType && givenType.GetGenericTypeDefinition() == genericType)
  21. {
  22. return true;
  23. }
  24. foreach (var interfaceType in givenType.GetInterfaces())
  25. {
  26. if (interfaceType.GetTypeInfo().IsGenericType && interfaceType.GetGenericTypeDefinition() == genericType)
  27. {
  28. return true;
  29. }
  30. }
  31. if (givenTypeInfo.BaseType == null)
  32. {
  33. return false;
  34. }
  35. return IsAssignableToGenericType(givenTypeInfo.BaseType, genericType);
  36. }
  37. /// <summary>
  38. /// Gets a list of attributes defined for a class member and it's declaring type including inherited attributes.
  39. /// </summary>
  40. /// <param name="inherit">Inherit attribute from base classes</param>
  41. /// <param name="memberInfo">MemberInfo</param>
  42. public static List<object> GetAttributesOfMemberAndDeclaringType(MemberInfo memberInfo, bool inherit = true)
  43. {
  44. var attributeList = new List<object>();
  45. attributeList.AddRange(memberInfo.GetCustomAttributes(inherit));
  46. if (memberInfo.DeclaringType != null)
  47. {
  48. attributeList.AddRange(memberInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(inherit));
  49. }
  50. return attributeList;
  51. }
  52. /// <summary>
  53. /// Gets a list of attributes defined for a class member and type including inherited attributes.
  54. /// </summary>
  55. /// <param name="memberInfo">MemberInfo</param>
  56. /// <param name="type">Type</param>
  57. /// <param name="inherit">Inherit attribute from base classes</param>
  58. public static List<object> GetAttributesOfMemberAndType(MemberInfo memberInfo, Type type, bool inherit = true)
  59. {
  60. var attributeList = new List<object>();
  61. attributeList.AddRange(memberInfo.GetCustomAttributes(inherit));
  62. attributeList.AddRange(type.GetTypeInfo().GetCustomAttributes(inherit));
  63. return attributeList;
  64. }
  65. /// <summary>
  66. /// Gets a list of attributes defined for a class member and it's declaring type including inherited attributes.
  67. /// </summary>
  68. /// <typeparam name="TAttribute">Type of the attribute</typeparam>
  69. /// <param name="memberInfo">MemberInfo</param>
  70. /// <param name="inherit">Inherit attribute from base classes</param>
  71. public static List<TAttribute> GetAttributesOfMemberAndDeclaringType<TAttribute>(MemberInfo memberInfo, bool inherit = true)
  72. where TAttribute : Attribute
  73. {
  74. var attributeList = new List<TAttribute>();
  75. if (memberInfo.IsDefined(typeof(TAttribute), inherit))
  76. {
  77. attributeList.AddRange(memberInfo.GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>());
  78. }
  79. if (memberInfo.DeclaringType != null && memberInfo.DeclaringType.GetTypeInfo().IsDefined(typeof(TAttribute), inherit))
  80. {
  81. attributeList.AddRange(memberInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>());
  82. }
  83. return attributeList;
  84. }
  85. /// <summary>
  86. /// Gets a list of attributes defined for a class member and type including inherited attributes.
  87. /// </summary>
  88. /// <typeparam name="TAttribute">Type of the attribute</typeparam>
  89. /// <param name="memberInfo">MemberInfo</param>
  90. /// <param name="type">Type</param>
  91. /// <param name="inherit">Inherit attribute from base classes</param>
  92. public static List<TAttribute> GetAttributesOfMemberAndType<TAttribute>(MemberInfo memberInfo, Type type, bool inherit = true)
  93. where TAttribute : Attribute
  94. {
  95. var attributeList = new List<TAttribute>();
  96. if (memberInfo.IsDefined(typeof(TAttribute), inherit))
  97. {
  98. attributeList.AddRange(memberInfo.GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>());
  99. }
  100. if (type.GetTypeInfo().IsDefined(typeof(TAttribute), inherit))
  101. {
  102. attributeList.AddRange(type.GetTypeInfo().GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>());
  103. }
  104. return attributeList;
  105. }
  106. /// <summary>
  107. /// Tries to gets an of attribute defined for a class member and it's declaring type including inherited attributes.
  108. /// Returns default value if it's not declared at all.
  109. /// </summary>
  110. /// <typeparam name="TAttribute">Type of the attribute</typeparam>
  111. /// <param name="memberInfo">MemberInfo</param>
  112. /// <param name="defaultValue">Default value (null as default)</param>
  113. /// <param name="inherit">Inherit attribute from base classes</param>
  114. public static TAttribute GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<TAttribute>(MemberInfo memberInfo, TAttribute defaultValue = default(TAttribute), bool inherit = true)
  115. where TAttribute : class
  116. {
  117. return memberInfo.GetCustomAttributes(true).OfType<TAttribute>().FirstOrDefault()
  118. ?? memberInfo.ReflectedType?.GetTypeInfo().GetCustomAttributes(true).OfType<TAttribute>().FirstOrDefault()
  119. ?? defaultValue;
  120. }
  121. /// <summary>
  122. /// Tries to gets an of attribute defined for a class member and it's declaring type including inherited attributes.
  123. /// Returns default value if it's not declared at all.
  124. /// </summary>
  125. /// <typeparam name="TAttribute">Type of the attribute</typeparam>
  126. /// <param name="memberInfo">MemberInfo</param>
  127. /// <param name="defaultValue">Default value (null as default)</param>
  128. /// <param name="inherit">Inherit attribute from base classes</param>
  129. public static TAttribute GetSingleAttributeOrDefault<TAttribute>(MemberInfo memberInfo, TAttribute defaultValue = default(TAttribute), bool inherit = true)
  130. where TAttribute : Attribute
  131. {
  132. //Get attribute on the member
  133. if (memberInfo.IsDefined(typeof(TAttribute), inherit))
  134. {
  135. return memberInfo.GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>().First();
  136. }
  137. return defaultValue;
  138. }
  139. /// <summary>
  140. /// Gets a property by it's full path from given object
  141. /// </summary>
  142. /// <param name="obj">Object to get value from</param>
  143. /// <param name="objectType">Type of given object</param>
  144. /// <param name="propertyPath">Full path of property</param>
  145. /// <returns></returns>
  146. internal static object GetPropertyByPath(object obj, Type objectType, string propertyPath)
  147. {
  148. var property = obj;
  149. var currentType = objectType;
  150. var objectPath = currentType.FullName;
  151. var absolutePropertyPath = propertyPath;
  152. if (absolutePropertyPath.StartsWith(objectPath))
  153. {
  154. absolutePropertyPath = absolutePropertyPath.Replace(objectPath + ".", "");
  155. }
  156. foreach (var propertyName in absolutePropertyPath.Split('.'))
  157. {
  158. property = currentType.GetProperty(propertyName);
  159. currentType = ((PropertyInfo) property).PropertyType;
  160. }
  161. return property;
  162. }
  163. /// <summary>
  164. /// Gets value of a property by it's full path from given object
  165. /// </summary>
  166. /// <param name="obj">Object to get value from</param>
  167. /// <param name="objectType">Type of given object</param>
  168. /// <param name="propertyPath">Full path of property</param>
  169. /// <returns></returns>
  170. internal static object GetValueByPath(object obj, Type objectType, string propertyPath)
  171. {
  172. var value = obj;
  173. var currentType = objectType;
  174. var objectPath = currentType.FullName;
  175. var absolutePropertyPath = propertyPath;
  176. if (absolutePropertyPath.StartsWith(objectPath))
  177. {
  178. absolutePropertyPath = absolutePropertyPath.Replace(objectPath + ".", "");
  179. }
  180. foreach (var propertyName in absolutePropertyPath.Split('.'))
  181. {
  182. var property = currentType.GetProperty(propertyName);
  183. value = property.GetValue(value, null);
  184. currentType = property.PropertyType;
  185. }
  186. return value;
  187. }
  188. /// <summary>
  189. /// Sets value of a property by it's full path on given object
  190. /// </summary>
  191. /// <param name="obj"></param>
  192. /// <param name="objectType"></param>
  193. /// <param name="propertyPath"></param>
  194. /// <param name="value"></param>
  195. internal static void SetValueByPath(object obj, Type objectType, string propertyPath, object value)
  196. {
  197. var currentType = objectType;
  198. PropertyInfo property;
  199. var objectPath = currentType.FullName;
  200. var absolutePropertyPath = propertyPath;
  201. if (absolutePropertyPath.StartsWith(objectPath))
  202. {
  203. absolutePropertyPath = absolutePropertyPath.Replace(objectPath + ".", "");
  204. }
  205. var properties = absolutePropertyPath.Split('.');
  206. if (properties.Length == 1)
  207. {
  208. property = objectType.GetProperty(properties.First());
  209. property.SetValue(obj, value);
  210. return;
  211. }
  212. for (int i = 0; i < properties.Length - 1; i++)
  213. {
  214. property = currentType.GetProperty(properties[i]);
  215. obj = property.GetValue(obj, null);
  216. currentType = property.PropertyType;
  217. }
  218. property = currentType.GetProperty(properties.Last());
  219. property.SetValue(obj, value);
  220. }
  221. internal static bool IsPropertyGetterSetterMethod(MethodInfo method, Type type)
  222. {
  223. if (!method.IsSpecialName)
  224. {
  225. return false;
  226. }
  227. if (method.Name.Length < 5)
  228. {
  229. return false;
  230. }
  231. return type.GetProperty(method.Name.Substring(4), BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic) != null;
  232. }
  233. }
  234. }