ReflectionUtils.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #region License
  2. // Copyright 2006 James Newton-King
  3. // http://www.newtonsoft.com
  4. //
  5. // This work is licensed under the Creative Commons Attribution 2.5 License
  6. // http://creativecommons.org/licenses/by/2.5/
  7. //
  8. // You are free:
  9. // * to copy, distribute, display, and perform the work
  10. // * to make derivative works
  11. // * to make commercial use of the work
  12. //
  13. // Under the following conditions:
  14. // * For any reuse or distribution, you must make clear to others the license terms of this work.
  15. // * Any of these conditions can be waived if you get permission from the copyright holder.
  16. #endregion
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Text;
  20. using System.Reflection;
  21. using System.Collections;
  22. using System.ComponentModel;
  23. namespace Newtonsoft.Json.Utilities
  24. {
  25. internal static class ReflectionUtils
  26. {
  27. public static bool IsInstantiatableType(Type t)
  28. {
  29. if (t == null)
  30. throw new ArgumentNullException("t");
  31. if (t.IsAbstract || t.IsInterface || t.IsArray)
  32. return false;
  33. if (!HasDefaultConstructor(t))
  34. return false;
  35. return true;
  36. }
  37. public static bool HasDefaultConstructor(Type t)
  38. {
  39. if (t == null)
  40. throw new ArgumentNullException("t");
  41. return (t.GetConstructor(BindingFlags.Instance, null, Type.EmptyTypes, null) != null);
  42. }
  43. public static bool IsAssignable (Type to, Type from) {
  44. if (to == null)
  45. throw new ArgumentNullException("to");
  46. if (to.IsAssignableFrom (from))
  47. return true;
  48. if (to.IsGenericType && from.IsGenericTypeDefinition)
  49. return to.IsAssignableFrom (from.MakeGenericType (to.GetGenericArguments ()));
  50. return false;
  51. }
  52. public static bool IsSubClass(Type type, Type check)
  53. {
  54. if (type == null || check == null)
  55. return false;
  56. if (type == check)
  57. return true;
  58. if (check.IsInterface)
  59. {
  60. foreach (Type t in type.GetInterfaces())
  61. {
  62. if (IsSubClass(t, check)) return true;
  63. }
  64. }
  65. if (type.IsGenericType && !type.IsGenericTypeDefinition)
  66. {
  67. if (IsSubClass(type.GetGenericTypeDefinition(), check))
  68. return true;
  69. }
  70. return IsSubClass(type.BaseType, check);
  71. }
  72. /// <summary>
  73. /// Gets the type of the typed list's items.
  74. /// </summary>
  75. /// <param name="type">The type.</param>
  76. /// <returns>The type of the typed list's items.</returns>
  77. public static Type GetTypedListItemType(Type type)
  78. {
  79. if (type == null)
  80. throw new ArgumentNullException("type");
  81. if (type.IsArray)
  82. return type.GetElementType ();
  83. else if (type.IsGenericType && typeof (List<>).IsAssignableFrom (type.GetGenericTypeDefinition ()))
  84. return type.GetGenericArguments () [0];
  85. else
  86. throw new Exception ("Bad type");
  87. }
  88. public static Type GetTypedDictionaryValueType(Type type)
  89. {
  90. if (type == null)
  91. throw new ArgumentNullException("type");
  92. Type genDictType = GetGenericDictionary(type);
  93. if (genDictType != null)
  94. return genDictType.GetGenericArguments () [1];
  95. else if (typeof(IDictionary).IsAssignableFrom(type))
  96. return null;
  97. else
  98. throw new Exception("Bad type");
  99. }
  100. static readonly Type GenericDictionaryType = typeof (IDictionary<,>);
  101. public static Type GetGenericDictionary (Type type) {
  102. if (type.IsGenericType && GenericDictionaryType.IsAssignableFrom (type.GetGenericTypeDefinition ()))
  103. return type;
  104. Type[] ifaces = type.GetInterfaces();
  105. if (ifaces != null)
  106. for (int i = 0; i < ifaces.Length; i++) {
  107. Type current = GetGenericDictionary (ifaces [i]);
  108. if (current != null)
  109. return current;
  110. }
  111. return null;
  112. }
  113. public static Type GetMemberUnderlyingType(MemberInfo member)
  114. {
  115. switch (member.MemberType)
  116. {
  117. case MemberTypes.Field:
  118. return ((FieldInfo)member).FieldType;
  119. case MemberTypes.Property:
  120. return ((PropertyInfo)member).PropertyType;
  121. case MemberTypes.Event:
  122. return ((EventInfo)member).EventHandlerType;
  123. default:
  124. throw new ArgumentException("MemberInfo must be if type FieldInfo, PropertyInfo or EventInfo", "member");
  125. }
  126. }
  127. /// <summary>
  128. /// Determines whether the member is an indexed property.
  129. /// </summary>
  130. /// <param name="member">The member.</param>
  131. /// <returns>
  132. /// <c>true</c> if the member is an indexed property; otherwise, <c>false</c>.
  133. /// </returns>
  134. public static bool IsIndexedProperty(MemberInfo member)
  135. {
  136. if (member == null)
  137. throw new ArgumentNullException("member");
  138. PropertyInfo propertyInfo = member as PropertyInfo;
  139. if (propertyInfo != null)
  140. return IsIndexedProperty(propertyInfo);
  141. else
  142. return false;
  143. }
  144. /// <summary>
  145. /// Determines whether the property is an indexed property.
  146. /// </summary>
  147. /// <param name="property">The property.</param>
  148. /// <returns>
  149. /// <c>true</c> if the property is an indexed property; otherwise, <c>false</c>.
  150. /// </returns>
  151. public static bool IsIndexedProperty(PropertyInfo property)
  152. {
  153. if (property == null)
  154. throw new ArgumentNullException("property");
  155. return (property.GetIndexParameters().Length > 0);
  156. }
  157. /// <summary>
  158. /// Gets the member's value on the object.
  159. /// </summary>
  160. /// <param name="member">The member.</param>
  161. /// <param name="target">The target object.</param>
  162. /// <returns>The member's value on the object.</returns>
  163. public static object GetMemberValue(MemberInfo member, object target)
  164. {
  165. switch (member.MemberType)
  166. {
  167. case MemberTypes.Field:
  168. return ((FieldInfo)member).GetValue(target);
  169. case MemberTypes.Property:
  170. try
  171. {
  172. return ((PropertyInfo)member).GetValue(target, null);
  173. }
  174. catch (TargetParameterCountException e)
  175. {
  176. throw new ArgumentException("MemberInfo has index parameters", "member", e);
  177. }
  178. default:
  179. throw new ArgumentException("MemberInfo is not of type FieldInfo or PropertyInfo", "member");
  180. }
  181. }
  182. /// <summary>
  183. /// Sets the member's value on the target object.
  184. /// </summary>
  185. /// <param name="member">The member.</param>
  186. /// <param name="target">The target.</param>
  187. /// <param name="value">The value.</param>
  188. public static void SetMemberValue(MemberInfo member, object target, object value)
  189. {
  190. switch (member.MemberType)
  191. {
  192. case MemberTypes.Field:
  193. ((FieldInfo)member).SetValue(target, value);
  194. break;
  195. case MemberTypes.Property:
  196. ((PropertyInfo)member).SetValue(target, value, null);
  197. break;
  198. default:
  199. throw new ArgumentException("MemberInfo must be if type FieldInfo or PropertyInfo", "member");
  200. }
  201. }
  202. /// <summary>
  203. /// Determines whether the specified MemberInfo can be read.
  204. /// </summary>
  205. /// <param name="member">The MemberInfo to determine whether can be read.</param>
  206. /// <returns>
  207. /// <c>true</c> if the specified MemberInfo can be read; otherwise, <c>false</c>.
  208. /// </returns>
  209. public static bool CanReadMemberValue(MemberInfo member)
  210. {
  211. switch (member.MemberType)
  212. {
  213. case MemberTypes.Field:
  214. return true;
  215. case MemberTypes.Property:
  216. return ((PropertyInfo) member).CanRead;
  217. default:
  218. return false;
  219. }
  220. }
  221. /// <summary>
  222. /// Determines whether the specified MemberInfo can be set.
  223. /// </summary>
  224. /// <param name="member">The MemberInfo to determine whether can be set.</param>
  225. /// <returns>
  226. /// <c>true</c> if the specified MemberInfo can be set; otherwise, <c>false</c>.
  227. /// </returns>
  228. public static bool CanSetMemberValue(MemberInfo member)
  229. {
  230. switch (member.MemberType)
  231. {
  232. case MemberTypes.Field:
  233. return true;
  234. case MemberTypes.Property:
  235. return ((PropertyInfo)member).CanWrite;
  236. default:
  237. return false;
  238. }
  239. }
  240. public static IEnumerable<MemberInfo> GetFieldsAndProperties (Type type, BindingFlags bindingAttr) {
  241. MemberInfo [] members = type.GetFields (bindingAttr);
  242. for (int i = 0; i < members.Length; i++)
  243. yield return members [i];
  244. members = type.GetProperties (bindingAttr);
  245. for (int i = 0; i < members.Length; i++)
  246. yield return members [i];
  247. }
  248. }
  249. }