ReflectionHelper.cs 6.3 KB

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