RealWorldTerrainReflectionHelper.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* INFINITY CODE 2013-2019 */
  2. /* http://www.infinity-code.com */
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. namespace InfinityCode.RealWorldTerrain.Utils
  8. {
  9. /// <summary>
  10. /// Helper class for compatibility of reflection on WSA.
  11. /// </summary>
  12. public static class RealWorldTerrainReflectionHelper
  13. {
  14. private const BindingFlags DefaultLookup = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
  15. /// <summary>
  16. /// Checks whether the type is anonymous.
  17. /// </summary>
  18. /// <param name="type">Type</param>
  19. /// <returns>True - type is anonymous, false - otherwise</returns>
  20. public static bool CheckIfAnonymousType(Type type)
  21. {
  22. if (type == null) throw new ArgumentNullException("type");
  23. return IsGenericType(type)
  24. && (type.Name.Contains("AnonymousType") || type.Name.Contains("AnonType"))
  25. && (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$"))
  26. && (GetAttributes(type) & TypeAttributes.NotPublic) == TypeAttributes.NotPublic;
  27. }
  28. /// <summary>
  29. /// Gets the attributes associated with the Type.
  30. /// </summary>
  31. /// <param name="type">Type</param>
  32. /// <returns>Attributes of type.</returns>
  33. public static TypeAttributes GetAttributes(Type type)
  34. {
  35. #if !NETFX_CORE
  36. return type.Attributes;
  37. #else
  38. return type.GetTypeInfo().Attributes;
  39. #endif
  40. }
  41. /// <summary>
  42. /// Searches for the fields defined for the current Type, using the specified binding constraints.
  43. /// </summary>
  44. /// <param name="type">Type</param>
  45. /// <param name="bindingAttr">A bitmask comprised of one or more BindingFlags that specify how the search is conducted.</param>
  46. /// <returns>An array of FieldInfo objects representing all fields defined for the current Type that match the specified binding constraints.</returns>
  47. public static IEnumerable<FieldInfo> GetFields(Type type, BindingFlags bindingAttr = DefaultLookup)
  48. {
  49. #if !NETFX_CORE
  50. return type.GetFields(bindingAttr);
  51. #else
  52. return type.GetTypeInfo().DeclaredFields;
  53. #endif
  54. }
  55. /// <summary>
  56. /// Returns an array of Type objects that represent the type arguments of a generic type or the type parameters of a generic type definition.
  57. /// </summary>
  58. /// <param name="type">Type</param>
  59. /// <returns>An array of Type objects that represent the type arguments of a generic type. Returns an empty array if the current type is not a generic type.</returns>
  60. public static Type[] GetGenericArguments(Type type)
  61. {
  62. #if !NETFX_CORE
  63. return type.GetGenericArguments();
  64. #else
  65. return type.GetTypeInfo().GenericTypeArguments;
  66. #endif
  67. }
  68. /// <summary>
  69. /// Searches for the public members with the specified name.
  70. /// </summary>
  71. /// <param name="type">Type</param>
  72. /// <param name="name">The String containing the name of the public members to get. </param>
  73. /// <returns>An array of MemberInfo objects representing the public members with the specified name, if found; otherwise, an empty array.</returns>
  74. public static MemberInfo GetMember(Type type, string name)
  75. {
  76. #if !NETFX_CORE
  77. MemberInfo[] infos = type.GetMember(name);
  78. if (infos.Length > 0) return infos[0];
  79. return null;
  80. #else
  81. IEnumerable<MemberInfo> members = type.GetTypeInfo().DeclaredMembers;
  82. foreach (MemberInfo member in members) if (member.Name == name) return member;
  83. return null;
  84. #endif
  85. }
  86. /// <summary>
  87. /// searches for the members defined for the current Type, using the specified binding constraints.
  88. /// </summary>
  89. /// <param name="type">Type</param>
  90. /// <param name="bindingAttr">A bitmask comprised of one or more BindingFlags that specify how the search is conducted.</param>
  91. /// <returns>An array of MemberInfo objects representing all members defined for the current Type that match the specified binding constraints.</returns>
  92. public static IEnumerable<MemberInfo> GetMembers(Type type, BindingFlags bindingAttr = DefaultLookup)
  93. {
  94. #if !NETFX_CORE
  95. return type.GetMembers(bindingAttr);
  96. #else
  97. return type.GetTypeInfo().DeclaredMembers;
  98. #endif
  99. }
  100. /// <summary>
  101. /// Gets a MemberTypes value indicating the type of the member — method, constructor, event, and so on.
  102. /// </summary>
  103. /// <param name="member">MemberInfo</param>
  104. /// <returns>MemberTypes value</returns>
  105. public static MemberTypes GetMemberType(MemberInfo member)
  106. {
  107. #if !NETFX_CORE
  108. return member.MemberType;
  109. #else
  110. if (member is PropertyInfo) return MemberTypes.Property;
  111. if (member is FieldInfo) return MemberTypes.Field;
  112. if (member is MethodInfo) return MemberTypes.Method;
  113. if (member is EventInfo) return MemberTypes.Event;
  114. if (member is ConstructorInfo) return MemberTypes.Constructor;
  115. return MemberTypes.All;
  116. #endif
  117. }
  118. /// <summary>
  119. /// Searches for the public method with the specified name.
  120. /// </summary>
  121. /// <param name="type">Type</param>
  122. /// <param name="name">The String containing the name of the public method to get. </param>
  123. /// <returns>A MethodInfo object representing the public method with the specified name, if found; otherwise, null.</returns>
  124. public static MethodInfo GetMethod(Type type, string name)
  125. {
  126. #if !NETFX_CORE
  127. return type.GetMethod(name);
  128. #else
  129. return type.GetTypeInfo().GetDeclaredMethod(name);
  130. #endif
  131. }
  132. /// <summary>
  133. /// Searches for the specified public method whose parameters match the specified argument types.
  134. /// </summary>
  135. /// <param name="type">Type</param>
  136. /// <param name="name">The String containing the name of the public method to get. </param>
  137. /// <param name="types">An array of Type objects representing the number, order, and type of the parameters for the method to get.</param>
  138. /// <returns>A MethodInfo object representing the public method whose parameters match the specified argument types, if found; otherwise, null.</returns>
  139. public static MethodInfo GetMethod(Type type, string name, Type[] types)
  140. {
  141. #if !NETFX_CORE
  142. return type.GetMethod(name, types);
  143. #else
  144. var methods = type.GetTypeInfo().GetDeclaredMethods(name);
  145. foreach(var m in methods)
  146. {
  147. var parms = m.GetParameters();
  148. if (parms != null && parms.Length == types.Length && parms[0].ParameterType == typeof(string))
  149. {
  150. bool success = true;
  151. for(int i = 0; i < parms.Length; i++)
  152. {
  153. if (parms[i].ParameterType != types[i])
  154. {
  155. success = false;
  156. break;
  157. }
  158. }
  159. if (success) return m;
  160. }
  161. }
  162. return null;
  163. #endif
  164. }
  165. /// <summary>
  166. /// Returns all the public properties of the current Type.
  167. /// </summary>
  168. /// <param name="type">Type</param>
  169. /// <returns>An array of PropertyInfo objects representing all public properties of the current Type.</returns>
  170. public static PropertyInfo[] GetProperties(Type type)
  171. {
  172. #if !NETFX_CORE
  173. return type.GetProperties();
  174. #else
  175. return type.GetTypeInfo().DeclaredProperties.ToArray();
  176. #endif
  177. }
  178. /// <summary>
  179. /// Gets a value indicating whether the Type is a class; that is, not a value type or interface.
  180. /// </summary>
  181. /// <param name="type">Type</param>
  182. /// <returns>True if the Type is a class; otherwise, false.</returns>
  183. public static bool IsClass(Type type)
  184. {
  185. #if !NETFX_CORE
  186. return type.IsClass;
  187. #else
  188. return type.GetTypeInfo().IsClass;
  189. #endif
  190. }
  191. /// <summary>
  192. /// Gets a value indicating whether the current type is a generic type.
  193. /// </summary>
  194. /// <param name="type">Type</param>
  195. /// <returns>True if the current type is a generic type; otherwise, false.</returns>
  196. public static bool IsGenericType(Type type)
  197. {
  198. #if !NETFX_CORE
  199. return type.IsGenericType;
  200. #else
  201. return type.GetTypeInfo().IsGenericType;
  202. #endif
  203. }
  204. /// <summary>
  205. /// Gets a value indicating whether the Type is a value type.
  206. /// </summary>
  207. /// <param name="type">Type</param>
  208. /// <returns>True if the Type is a value type; otherwise, false.</returns>
  209. public static bool IsValueType(Type type)
  210. {
  211. #if !NETFX_CORE
  212. return type.IsValueType;
  213. #else
  214. return type.GetTypeInfo().IsValueType;
  215. #endif
  216. }
  217. }
  218. }