ReflectionExtensions.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.Serialization;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace ServiceStack
  10. {
  11. public static class ReflectionExtensions
  12. {
  13. public static bool IsInstanceOf(this Type type, Type thisOrBaseType)
  14. {
  15. while (type != null)
  16. {
  17. if (type == thisOrBaseType)
  18. return true;
  19. type = type.BaseType();
  20. }
  21. return false;
  22. }
  23. public static Type FirstGenericType(this Type type)
  24. {
  25. while (type != null)
  26. {
  27. if (type.IsGeneric())
  28. return type;
  29. type = type.BaseType();
  30. }
  31. return null;
  32. }
  33. public static Type GetTypeWithGenericTypeDefinitionOf(this Type type, Type genericTypeDefinition)
  34. {
  35. foreach (var t in type.GetTypeInterfaces())
  36. {
  37. if (t.IsGeneric() && t.GetGenericTypeDefinition() == genericTypeDefinition)
  38. {
  39. return t;
  40. }
  41. }
  42. var genericType = type.FirstGenericType();
  43. if (genericType != null && genericType.GetGenericTypeDefinition() == genericTypeDefinition)
  44. {
  45. return genericType;
  46. }
  47. return null;
  48. }
  49. public static PropertyInfo[] GetAllProperties(this Type type)
  50. {
  51. if (type.IsInterface())
  52. {
  53. var propertyInfos = new List<PropertyInfo>();
  54. var considered = new List<Type>();
  55. var queue = new Queue<Type>();
  56. considered.Add(type);
  57. queue.Enqueue(type);
  58. while (queue.Count > 0)
  59. {
  60. var subType = queue.Dequeue();
  61. foreach (var subInterface in subType.GetTypeInterfaces())
  62. {
  63. if (considered.Contains(subInterface)) continue;
  64. considered.Add(subInterface);
  65. queue.Enqueue(subInterface);
  66. }
  67. var typeProperties = subType.GetTypesProperties();
  68. var newPropertyInfos = typeProperties
  69. .Where(x => !propertyInfos.Contains(x));
  70. propertyInfos.InsertRange(0, newPropertyInfos);
  71. }
  72. return propertyInfos.ToArray();
  73. }
  74. return type.GetTypesProperties()
  75. .Where(t => t.GetIndexParameters().Length == 0) // ignore indexed properties
  76. .ToArray();
  77. }
  78. public static PropertyInfo[] GetPublicProperties(this Type type)
  79. {
  80. if (type.IsInterface())
  81. {
  82. var propertyInfos = new List<PropertyInfo>();
  83. var considered = new List<Type>();
  84. var queue = new Queue<Type>();
  85. considered.Add(type);
  86. queue.Enqueue(type);
  87. while (queue.Count > 0)
  88. {
  89. var subType = queue.Dequeue();
  90. foreach (var subInterface in subType.GetTypeInterfaces())
  91. {
  92. if (considered.Contains(subInterface)) continue;
  93. considered.Add(subInterface);
  94. queue.Enqueue(subInterface);
  95. }
  96. var typeProperties = subType.GetTypesPublicProperties();
  97. var newPropertyInfos = typeProperties
  98. .Where(x => !propertyInfos.Contains(x));
  99. propertyInfos.InsertRange(0, newPropertyInfos);
  100. }
  101. return propertyInfos.ToArray();
  102. }
  103. return type.GetTypesPublicProperties()
  104. .Where(t => t.GetIndexParameters().Length == 0) // ignore indexed properties
  105. .ToArray();
  106. }
  107. public const string DataMember = "DataMemberAttribute";
  108. internal static string[] IgnoreAttributesNamed = new[] {
  109. "IgnoreDataMemberAttribute",
  110. "JsonIgnoreAttribute"
  111. };
  112. public static PropertyInfo[] GetSerializableProperties(this Type type)
  113. {
  114. var properties = type.IsDto()
  115. ? type.GetAllProperties()
  116. : type.GetPublicProperties();
  117. return properties.OnlySerializableProperties(type);
  118. }
  119. private static List<Type> _excludeTypes = new List<Type> { typeof(Stream) };
  120. public static PropertyInfo[] OnlySerializableProperties(this PropertyInfo[] properties, Type type = null)
  121. {
  122. var isDto = type.IsDto();
  123. var readableProperties = properties.Where(x => x.PropertyGetMethod(nonPublic: isDto) != null);
  124. if (isDto)
  125. {
  126. return readableProperties.Where(attr =>
  127. attr.HasAttribute<DataMemberAttribute>()).ToArray();
  128. }
  129. // else return those properties that are not decorated with IgnoreDataMember
  130. return readableProperties
  131. .Where(prop => prop.AllAttributes()
  132. .All(attr =>
  133. {
  134. var name = attr.GetType().Name;
  135. return !IgnoreAttributesNamed.Contains(name);
  136. }))
  137. .Where(prop => !_excludeTypes.Contains(prop.PropertyType))
  138. .ToArray();
  139. }
  140. }
  141. public static class PlatformExtensions //Because WinRT is a POS
  142. {
  143. public static bool IsInterface(this Type type)
  144. {
  145. return type.GetTypeInfo().IsInterface;
  146. }
  147. public static bool IsGeneric(this Type type)
  148. {
  149. return type.GetTypeInfo().IsGenericType;
  150. }
  151. public static Type BaseType(this Type type)
  152. {
  153. return type.GetTypeInfo().BaseType;
  154. }
  155. public static Type[] GetTypeInterfaces(this Type type)
  156. {
  157. return type.GetTypeInfo().ImplementedInterfaces.ToArray();
  158. }
  159. internal static PropertyInfo[] GetTypesPublicProperties(this Type subType)
  160. {
  161. var pis = new List<PropertyInfo>();
  162. foreach (var pi in subType.GetRuntimeProperties())
  163. {
  164. var mi = pi.GetMethod ?? pi.SetMethod;
  165. if (mi != null && mi.IsStatic) continue;
  166. pis.Add(pi);
  167. }
  168. return pis.ToArray();
  169. }
  170. internal static PropertyInfo[] GetTypesProperties(this Type subType)
  171. {
  172. var pis = new List<PropertyInfo>();
  173. foreach (var pi in subType.GetRuntimeProperties())
  174. {
  175. var mi = pi.GetMethod ?? pi.SetMethod;
  176. if (mi != null && mi.IsStatic) continue;
  177. pis.Add(pi);
  178. }
  179. return pis.ToArray();
  180. }
  181. public static bool HasAttribute<T>(this Type type)
  182. {
  183. return type.AllAttributes().Any(x => x.GetType() == typeof(T));
  184. }
  185. public static bool HasAttribute<T>(this PropertyInfo pi)
  186. {
  187. return pi.AllAttributes().Any(x => x.GetType() == typeof(T));
  188. }
  189. public static bool IsDto(this Type type)
  190. {
  191. if (type == null)
  192. return false;
  193. return type.HasAttribute<DataContractAttribute>();
  194. }
  195. public static MethodInfo PropertyGetMethod(this PropertyInfo pi, bool nonPublic = false)
  196. {
  197. return pi.GetMethod;
  198. }
  199. public static object[] AllAttributes(this PropertyInfo propertyInfo)
  200. {
  201. return propertyInfo.GetCustomAttributes(true).ToArray();
  202. }
  203. public static object[] AllAttributes(this PropertyInfo propertyInfo, Type attrType)
  204. {
  205. return propertyInfo.GetCustomAttributes(true).Where(x => attrType.IsInstanceOf(x.GetType())).ToArray();
  206. }
  207. public static object[] AllAttributes(this Type type)
  208. {
  209. return type.GetTypeInfo().GetCustomAttributes(true).ToArray();
  210. }
  211. public static TAttr[] AllAttributes<TAttr>(this PropertyInfo pi)
  212. {
  213. return pi.AllAttributes(typeof(TAttr)).Cast<TAttr>().ToArray();
  214. }
  215. public static TAttr[] AllAttributes<TAttr>(this Type type)
  216. where TAttr : Attribute
  217. {
  218. return type.GetTypeInfo().GetCustomAttributes<TAttr>(true).ToArray();
  219. }
  220. }
  221. }