You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
7.6 KiB

2 years ago
  1. using System;
  2. using System.Reflection;
  3. namespace EasyNet.Common
  4. {
  5. public class ReflectionHelper
  6. {
  7. public static PropertyInfo[] GetProperties(Type type)
  8. {
  9. return type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
  10. }
  11. public static FieldInfo[] GetFields(Type type)
  12. {
  13. return type.GetFields(BindingFlags.Public | BindingFlags.Instance);
  14. }
  15. #region 快速執行方法
  16. /// <summary>
  17. /// 快速執行Method
  18. /// </summary>
  19. /// <param name="obj"></param>
  20. /// <param name="method"></param>
  21. /// <param name="parameters"></param>
  22. /// <returns></returns>
  23. public static object FastMethodInvoke(object obj, MethodInfo method, params object[] parameters)
  24. {
  25. return DynamicCalls.GetMethodInvoker(method)(obj, parameters);
  26. }
  27. /// <summary>
  28. /// 快速產生實體一個T
  29. /// </summary>
  30. /// <typeparam name="T"></typeparam>
  31. /// <returns></returns>
  32. public static T Create<T>()
  33. {
  34. return (T)Create(typeof(T))();
  35. }
  36. /// <summary>
  37. /// 快速產生實體一個FastCreateInstanceHandler
  38. /// </summary>
  39. /// <param name="type"></param>
  40. /// <returns></returns>
  41. public static FastCreateInstanceHandler Create(Type type)
  42. {
  43. return DynamicCalls.GetInstanceCreator(type);
  44. }
  45. #endregion 快速執行方法
  46. #region 設置屬性值
  47. /// <summary>
  48. /// 設置屬性值
  49. /// </summary>
  50. /// <param name="obj"></param>
  51. /// <param name="property"></param>
  52. /// <param name="value"></param>
  53. public static void SetPropertyValue(object obj, PropertyInfo property, object value)
  54. {
  55. if (property.CanWrite)
  56. {
  57. var propertySetter = DynamicCalls.GetPropertySetter(property);
  58. value = TypeUtils.ConvertForType(value, property.PropertyType);
  59. propertySetter(obj, value);
  60. }
  61. }
  62. /// <summary>
  63. /// 設置屬性值
  64. /// </summary>
  65. /// <param name="obj"></param>
  66. /// <param name="propertyName"></param>
  67. /// <param name="value"></param>
  68. public static void SetPropertyValue(object obj, string propertyName, object value)
  69. {
  70. SetPropertyValue(obj.GetType(), obj, propertyName, value);
  71. }
  72. /// <summary>
  73. /// 設置屬性值
  74. /// </summary>
  75. /// <param name="type"></param>
  76. /// <param name="obj"></param>
  77. /// <param name="propertyName"></param>
  78. /// <param name="value"></param>
  79. public static void SetPropertyValue(Type type, object obj, string propertyName, object value)
  80. {
  81. var property = type.GetProperty(propertyName);
  82. if (property != null)
  83. {
  84. SetPropertyValue(obj, property, value);
  85. }
  86. }
  87. /// <summary>
  88. /// 獲取屬性值
  89. /// </summary>
  90. /// <typeparam name="T"></typeparam>
  91. /// <param name="entity"></param>
  92. /// <param name="propertyName"></param>
  93. /// <returns></returns>
  94. public static object GetPropertyValue<T>(T entity, string propertyName)
  95. {
  96. var property = entity.GetType().GetProperty(propertyName);
  97. if (property != null)
  98. {
  99. return property.GetValue(entity, null);
  100. }
  101. return null;
  102. }
  103. /// <summary>
  104. /// 獲取屬性值
  105. /// </summary>
  106. /// <param name="entity"></param>
  107. /// <param name="property">todo: describe property parameter on GetPropertyValue</param>
  108. /// <typeparam name="T"></typeparam>
  109. /// <returns></returns>
  110. public static object GetPropertyValue<T>(T entity, PropertyInfo property)
  111. {
  112. if (property != null)
  113. {
  114. return property.GetValue(entity, null);
  115. }
  116. return null;
  117. }
  118. /// <summary>
  119. /// 獲取屬性類型
  120. /// </summary>
  121. /// <typeparam name="T"></typeparam>
  122. /// <param name="entity"></param>
  123. /// <param name="propertyName"></param>
  124. /// <returns></returns>
  125. public static string GetPropertyType<T>(T entity, string propertyName)
  126. {
  127. var property = entity.GetType().GetProperty(propertyName);
  128. if (property != null)
  129. {
  130. return property.PropertyType.FullName;
  131. }
  132. return null;
  133. }
  134. /// <summary>
  135. /// 獲取屬性類型
  136. /// </summary>
  137. /// <param name="propertyName"></param>
  138. /// <param name="properties">todo: describe properties parameter on GetProperty</param>
  139. /// <typeparam name="T"></typeparam>
  140. /// <returns></returns>
  141. public static PropertyInfo GetProperty(PropertyInfo[] properties, string propertyName)
  142. {
  143. foreach (PropertyInfo property in properties)
  144. {
  145. var name = property.Name;
  146. if (propertyName.ToLower() == name.ToLower())
  147. {
  148. return property;
  149. }
  150. }
  151. return null;
  152. }
  153. /// <summary>
  154. /// 轉換值
  155. /// </summary>
  156. /// <param name="entity">todo: describe entity parameter on ConvertPropertyValue</param>
  157. /// <param name="propertyName">todo: describe propertyName parameter on ConvertPropertyValue</param>
  158. public static object ConvertPropertyValue<T>(T entity, string propertyName)
  159. {
  160. var property = entity.GetType().GetProperty(propertyName);
  161. if (property != null)
  162. {
  163. if (property.CanWrite)
  164. {
  165. return TypeUtils.ConvertForType(property.GetValue(entity, null), property.PropertyType);
  166. }
  167. }
  168. return null;
  169. }
  170. #endregion 設置屬性值
  171. /*public static void SetPropertyValue(Object obj, PropertyInfo property, Object value)
  172. {
  173. //創建Set委託
  174. SetHandler setter = DynamicMethodCompiler.CreateSetHandler(obj.GetType(),property);
  175. //先獲取該私有成員的資料類型
  176. Type type = property.PropertyType;
  177. //通過資料類型轉換
  178. value = TypeUtils.ConvertForType(value, type);
  179. //將值設置到對象中
  180. setter(obj, value);
  181. }
  182. public static Object GetPropertyValue(Object obj, PropertyInfo property)
  183. {
  184. //創建Set委託
  185. GetHandler getter = DynamicMethodCompiler.CreateGetHandler(obj.GetType(), property);
  186. //獲取屬性值
  187. return getter(obj);
  188. }
  189. public static void SetFieldValue(Object obj, FieldInfo field, Object value)
  190. {
  191. //創建Set委託
  192. SetHandler setter = DynamicMethodCompiler.CreateSetHandler(obj.GetType(), field);
  193. //先獲取該私有成員的資料類型
  194. Type type = field.FieldType;
  195. //通過資料類型轉換
  196. value = TypeUtils.ConvertForType(value, type);
  197. //將值設置到對象中
  198. setter(obj, value);
  199. }
  200. public static Object GetFieldValue(Object obj, FieldInfo field)
  201. {
  202. //創建Set委託
  203. GetHandler getter = DynamicMethodCompiler.CreateGetHandler(obj.GetType(), field);
  204. //獲取欄位值
  205. return getter(obj);
  206. }*/
  207. }
  208. }