You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
12 KiB

4 years ago
4 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Reflection.Emit;
  5. namespace IPA.Utilities
  6. {
  7. /// <summary>
  8. /// A type containing utilities for accessing non-public fields of objects.
  9. /// </summary>
  10. /// <typeparam name="T">the type that the fields are on</typeparam>
  11. /// <typeparam name="U">the type of the field to access</typeparam>
  12. /// <seealso cref="PropertyAccessor{T, U}"/>
  13. public static class FieldAccessor<T, U>
  14. {
  15. /// <summary>
  16. /// A delegate for a field accessor taking a <typeparamref name="T"/> ref and returning a <typeparamref name="U"/> ref.
  17. /// </summary>
  18. /// <param name="obj">the object to access the field of</param>
  19. /// <returns>a reference to the field's value</returns>
  20. public delegate ref U Accessor(ref T obj);
  21. // field name -> accessor
  22. private static readonly Dictionary<string, Accessor> accessors = new Dictionary<string, Accessor>();
  23. private static Accessor MakeAccessor(string fieldName)
  24. {
  25. var field = typeof(T).GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
  26. if (field == null)
  27. throw new MissingFieldException(typeof(T).Name, fieldName);
  28. var dynMethodName = $"<>_accessor__{fieldName}";
  29. // unfortunately DynamicMethod doesn't like having a ByRef return type, so reflection it
  30. var dyn = new DynamicMethod(dynMethodName, typeof(U), new[] { typeof(T).MakeByRefType() }, typeof(FieldAccessor<T, U>), true);
  31. ReflectionUtil.DynamicMethodReturnType.SetValue(dyn, typeof(U).MakeByRefType());
  32. var il = dyn.GetILGenerator();
  33. il.Emit(OpCodes.Ldarg_0);
  34. if (!typeof(T).IsValueType)
  35. il.Emit(OpCodes.Ldind_Ref);
  36. il.Emit(OpCodes.Ldflda, field);
  37. il.Emit(OpCodes.Ret);
  38. return (Accessor)dyn.CreateDelegate(typeof(Accessor));
  39. }
  40. /// <summary>
  41. /// Gets an <see cref="Accessor"/> for the field named <paramref name="name"/> on <typeparamref name="T"/>.
  42. /// </summary>
  43. /// <param name="name">the field name</param>
  44. /// <returns>an accessor for the field</returns>
  45. /// <exception cref="MissingFieldException">if the field does not exist on <typeparamref name="T"/></exception>
  46. public static Accessor GetAccessor(string name)
  47. {
  48. if (!accessors.TryGetValue(name, out var accessor))
  49. accessors.Add(name, accessor = MakeAccessor(name));
  50. return accessor;
  51. }
  52. /// <summary>
  53. /// Accesses a field for an object by name.
  54. /// </summary>
  55. /// <param name="obj">the object to access the field of</param>
  56. /// <param name="name">the name of the field to access</param>
  57. /// <returns>a reference to the object at the field</returns>
  58. /// <exception cref="MissingFieldException">if the field does not exist on <typeparamref name="T"/></exception>
  59. /// <seealso cref="GetAccessor(string)"/>
  60. public static ref U Access(ref T obj, string name) => ref GetAccessor(name)(ref obj);
  61. /// <summary>
  62. /// Gets the value of a field of an object by name.
  63. /// </summary>
  64. /// <remarks>
  65. /// The only good reason to use this over <see cref="Get(T, string)"/> is when you are working with a value type,
  66. /// as it prevents a copy.
  67. /// </remarks>
  68. /// <param name="obj">the object to access the field of</param>
  69. /// <param name="name">the name of the field to access</param>
  70. /// <returns>the value of the field</returns>
  71. /// <exception cref="MissingFieldException">if the field does not exist on <typeparamref name="T"/></exception>
  72. /// <seealso cref="Get(T, string)"/>
  73. /// <seealso cref="Access(ref T, string)"/>
  74. /// <seealso cref="GetAccessor(string)"/>
  75. public static U Get(ref T obj, string name) => Access(ref obj, name);
  76. /// <summary>
  77. /// Gets the value of a field of an object by name.
  78. /// </summary>
  79. /// <param name="obj">the object to access the field of</param>
  80. /// <param name="name">the name of the field to access</param>
  81. /// <returns>the value of the field</returns>
  82. /// <exception cref="MissingFieldException">if the field does not exist on <typeparamref name="T"/></exception>
  83. /// <seealso cref="Get(ref T, string)"/>
  84. /// <seealso cref="Access(ref T, string)"/>
  85. /// <seealso cref="GetAccessor(string)"/>
  86. public static U Get(T obj, string name) => Get(ref obj, name);
  87. /// <summary>
  88. /// Sets the value of a field for an object by name.
  89. /// </summary>
  90. /// <remarks>
  91. /// This overload must be used for value types.
  92. /// </remarks>
  93. /// <param name="obj">the object to set the field of</param>
  94. /// <param name="name">the name of the field</param>
  95. /// <param name="value">the value to set it to</param>
  96. /// <exception cref="MissingFieldException">if the field does not exist on <typeparamref name="T"/></exception>
  97. /// <seealso cref="Set(T, string, U)"/>
  98. /// <seealso cref="Access(ref T, string)"/>
  99. /// <seealso cref="GetAccessor(string)"/>
  100. public static void Set(ref T obj, string name, U value) => Access(ref obj, name) = value;
  101. /// <summary>
  102. /// Sets the value of a field for an object by name.
  103. /// </summary>
  104. /// <remarks>
  105. /// This overload cannot be safely used for value types. Use <see cref="Set(ref T, string, U)"/> instead.
  106. /// </remarks>
  107. /// <param name="obj">the object to set the field of</param>
  108. /// <param name="name">the name of the field</param>
  109. /// <param name="value">the value to set it to</param>
  110. /// <exception cref="MissingFieldException">if the field does not exist on <typeparamref name="T"/></exception>
  111. /// <seealso cref="Set(ref T, string, U)"/>
  112. /// <seealso cref="Access(ref T, string)"/>
  113. /// <seealso cref="GetAccessor(string)"/>
  114. public static void Set(T obj, string name, U value) => Set(ref obj, name, value);
  115. }
  116. /// <summary>
  117. /// A type containing utilities for accessing non-public properties of an object.
  118. /// </summary>
  119. /// <typeparam name="T">the type that the properties are on</typeparam>
  120. /// <typeparam name="U">the type of the property to access</typeparam>
  121. public static class PropertyAccessor<T, U>
  122. {
  123. /// <summary>
  124. /// A getter for a property.
  125. /// </summary>
  126. /// <param name="obj">the object it is a member of</param>
  127. /// <returns>the value of the property</returns>
  128. public delegate U Getter(T obj);
  129. /// <summary>
  130. /// A setter for a property.
  131. /// </summary>
  132. /// <param name="obj">the object it is a member of</param>
  133. /// <param name="val">the new property value</param>
  134. public delegate void Setter(T obj, U val);
  135. private static readonly Dictionary<string, (Getter get, Setter set)> props = new Dictionary<string, (Getter get, Setter set)>();
  136. private static (Getter, Setter) MakeAccessors(string propName)
  137. {
  138. var prop = typeof(T).GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
  139. if (prop == null)
  140. throw new MissingMemberException(typeof(T).Name, propName);
  141. var getM = prop.GetGetMethod(true);
  142. var setM = prop.GetSetMethod(true);
  143. Getter getter = null;
  144. Setter setter = null;
  145. if (getM != null)
  146. getter = (Getter)Delegate.CreateDelegate(typeof(Getter), getM);
  147. if (setM != null)
  148. setter = (Setter)Delegate.CreateDelegate(typeof(Setter), setM);
  149. return (getter, setter);
  150. }
  151. private static (Getter get, Setter set) GetAccessors(string propName)
  152. {
  153. if (!props.TryGetValue(propName, out var access))
  154. props.Add(propName, access = MakeAccessors(propName));
  155. return access;
  156. }
  157. /// <summary>
  158. /// Gets a <see cref="Getter"/> for the property identified by <paramref name="name"/>.
  159. /// </summary>
  160. /// <param name="name">the name of the property</param>
  161. /// <returns>a <see cref="Getter"/> that can access that property</returns>
  162. /// <exception cref="MissingMemberException">when the property does not exist</exception>
  163. public static Getter GetGetter(string name) => GetAccessors(name).get;
  164. /// <summary>
  165. /// Gets a <see cref="Setter"/> for the property identified by <paramref name="name"/>.
  166. /// </summary>
  167. /// <param name="name">the name of the property</param>
  168. /// <returns>a <see cref="Setter"/> that can access that property</returns>
  169. /// <exception cref="MissingMemberException">when the property does not exist</exception>
  170. public static Setter GetSetter(string name) => GetAccessors(name).set;
  171. /// <summary>
  172. /// Gets the value of the property identified by <paramref name="name"/> on <paramref name="obj"/>.
  173. /// </summary>
  174. /// <remarks>
  175. /// The only reason to use this over <see cref="Get(T, string)"/> is if you are using a value type because
  176. /// it avoids a copy.
  177. /// </remarks>
  178. /// <param name="obj">the instance to access</param>
  179. /// <param name="name">the name of the property</param>
  180. /// <returns>the value of the property</returns>
  181. /// <exception cref="MissingMemberException">when the property does not exist</exception>
  182. /// <seealso cref="Get(T, string)"/>
  183. /// <seealso cref="GetGetter(string)"/>
  184. public static U Get(ref T obj, string name) => GetGetter(name)(obj);
  185. /// <summary>
  186. /// Gets the value of the property identified by <paramref name="name"/> on <paramref name="obj"/>.
  187. /// </summary>
  188. /// <param name="obj">the instance to access</param>
  189. /// <param name="name">the name of the property</param>
  190. /// <returns>the value of the property</returns>
  191. /// <exception cref="MissingMemberException">when the property does not exist</exception>
  192. /// <seealso cref="Get(ref T, string)"/>
  193. /// <seealso cref="GetGetter(string)"/>
  194. public static U Get(T obj, string name) => GetGetter(name)(obj);
  195. /// <summary>
  196. /// Sets the value of the property identified by <paramref name="name"/> on <paramref name="obj"/>.
  197. /// </summary>
  198. /// <remarks>
  199. /// This overload must be used for value types.
  200. /// </remarks>
  201. /// <param name="obj">the instance to access</param>
  202. /// <param name="name">the name of the property</param>
  203. /// <param name="val">the new value of the property</param>
  204. /// <exception cref="MissingMemberException">when the property does not exist</exception>
  205. /// <seealso cref="Set(T, string, U)"/>
  206. /// <seealso cref="GetSetter(string)"/>
  207. public static void Set(ref T obj, string name, U val) => GetSetter(name)(obj, val);
  208. /// <summary>
  209. /// Sets the value of the property identified by <paramref name="name"/> on <paramref name="obj"/>.
  210. /// </summary>
  211. /// <remarks>
  212. /// This overload cannot be safely used for value types. Use <see cref="Set(ref T, string, U)"/> instead.
  213. /// </remarks>
  214. /// <param name="obj">the instance to access</param>
  215. /// <param name="name">the name of the property</param>
  216. /// <param name="val">the new value of the property</param>
  217. /// <exception cref="MissingMemberException">when the property does not exist</exception>
  218. /// <seealso cref="Set(ref T, string, U)"/>
  219. /// <seealso cref="GetSetter(string)"/>
  220. public static void Set(T obj, string name, U val) => GetSetter(name)(obj, val);
  221. }
  222. }