ReflectionHelper.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using IwbZero.ToolCommon.LogHelpers;
  5. namespace IwbZero.ToolCommon
  6. {
  7. public static class ReflectionHelper
  8. {
  9. public static TOut GetFiledValue<TOut>(this object obj, string filedName)
  10. {
  11. try
  12. {
  13. var value = obj.GetType().GetProperty(filedName)?.GetValue(obj, null);
  14. return (TOut)value;
  15. }
  16. catch (Exception e)
  17. {
  18. typeof(ReflectionHelper).LogError(e);
  19. return default(TOut);
  20. }
  21. }
  22. public static TOut SetFiledValue<TOut, TFiled>(this TOut obj, string filedName, TFiled value)
  23. {
  24. try
  25. {
  26. var filed = obj.GetType().GetProperty(filedName);
  27. if (filed != null)
  28. {
  29. filed.SetValue(obj, value);
  30. }
  31. }
  32. catch (Exception e)
  33. {
  34. typeof(ReflectionHelper).LogError(e);
  35. }
  36. return obj;
  37. }
  38. public static Type TypenFromAssembly(this Assembly assembly, string typeName)
  39. {
  40. Type[] typeArray = assembly.GetTypes();
  41. foreach (var type in typeArray)
  42. {
  43. if ((type.FullName != null && type.FullName.Equals(typeName)) || type.Name.Equals(typeName))
  44. {
  45. return type;
  46. }
  47. }
  48. return null;
  49. }
  50. public static TAttribute GetSingleAttribute<TAttribute>(this Type type, TAttribute defaultValue = default(TAttribute), bool inherit = true)
  51. where TAttribute : Attribute
  52. {
  53. return type.GetCustomAttributes(inherit).OfType<TAttribute>().FirstOrDefault()
  54. ?? type.ReflectedType?.GetTypeInfo().GetCustomAttributes(inherit).OfType<TAttribute>().FirstOrDefault()
  55. ?? defaultValue;
  56. }
  57. public static TAttribute GetMemberSingleAttribute<TAttribute>(this MemberInfo memberInfo, TAttribute defaultValue = default(TAttribute), bool inherit = true)
  58. where TAttribute : Attribute
  59. {
  60. //Get attribute on the member
  61. if (memberInfo.IsDefined(typeof(TAttribute), inherit))
  62. {
  63. return memberInfo.GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>().First();
  64. }
  65. return defaultValue;
  66. }
  67. /// <summary>
  68. /// Gets a property by it's full path from given object
  69. /// </summary>
  70. /// <param name="obj">Object to get value from</param>
  71. /// <param name="objectType">Type of given object</param>
  72. /// <param name="propertyPath">Full path of property</param>
  73. /// <returns></returns>
  74. internal static object GetPropertyByPath(object obj, Type objectType, string propertyPath)
  75. {
  76. var property = obj;
  77. var currentType = objectType;
  78. var objectPath = currentType.FullName;
  79. var absolutePropertyPath = propertyPath;
  80. if (absolutePropertyPath.StartsWith(objectPath ?? throw new InvalidOperationException()))
  81. {
  82. absolutePropertyPath = absolutePropertyPath.Replace(objectPath + ".", "");
  83. }
  84. foreach (var propertyName in absolutePropertyPath.Split('.'))
  85. {
  86. if (currentType != null) property = currentType.GetProperty(propertyName);
  87. currentType = ((PropertyInfo)property)?.PropertyType;
  88. }
  89. return property;
  90. }
  91. /// <summary>
  92. /// Gets value of a property by it's full path from given object
  93. /// </summary>
  94. /// <param name="obj">Object to get value from</param>
  95. /// <param name="objectType">Type of given object</param>
  96. /// <param name="propertyPath">Full path of property</param>
  97. /// <returns></returns>
  98. internal static object GetValueByPath(object obj, Type objectType, string propertyPath)
  99. {
  100. var value = obj;
  101. var currentType = objectType;
  102. var objectPath = currentType.FullName;
  103. var absolutePropertyPath = propertyPath;
  104. if (absolutePropertyPath.StartsWith(objectPath ?? throw new InvalidOperationException()))
  105. {
  106. absolutePropertyPath = absolutePropertyPath.Replace(objectPath + ".", "");
  107. }
  108. foreach (var propertyName in absolutePropertyPath.Split('.'))
  109. {
  110. var property = currentType.GetProperty(propertyName);
  111. if (property != null)
  112. {
  113. value = property.GetValue(value, null);
  114. currentType = property.PropertyType;
  115. }
  116. }
  117. return value;
  118. }
  119. /// <summary>
  120. /// Sets value of a property by it's full path on given object
  121. /// </summary>
  122. /// <param name="obj"></param>
  123. /// <param name="objectType"></param>
  124. /// <param name="propertyPath"></param>
  125. /// <param name="value"></param>
  126. internal static void SetValueByPath(object obj, Type objectType, string propertyPath, object value)
  127. {
  128. var currentType = objectType;
  129. PropertyInfo property;
  130. var objectPath = currentType.FullName;
  131. var absolutePropertyPath = propertyPath;
  132. if (absolutePropertyPath.StartsWith(objectPath ?? throw new InvalidOperationException()))
  133. {
  134. absolutePropertyPath = absolutePropertyPath.Replace(objectPath + ".", "");
  135. }
  136. var properties = absolutePropertyPath.Split('.');
  137. if (properties.Length == 1)
  138. {
  139. property = objectType.GetProperty(properties.First());
  140. if (property != null) property.SetValue(obj, value);
  141. return;
  142. }
  143. for (int i = 0; i < properties.Length - 1; i++)
  144. {
  145. property = currentType.GetProperty(properties[i]);
  146. if (property != null)
  147. {
  148. obj = property.GetValue(obj, null);
  149. currentType = property.PropertyType;
  150. }
  151. }
  152. property = currentType.GetProperty(properties.Last());
  153. if (property != null) property.SetValue(obj, value);
  154. }
  155. }
  156. }