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.

175 lines
9.7 KiB

  1. using System;
  2. using System.Reflection;
  3. using System.Reflection.Emit;
  4. using UnityEngine;
  5. namespace IPA.Utilities
  6. {
  7. /// <summary>
  8. /// A utility class providing reflection helper methods.
  9. /// </summary>
  10. public static partial class ReflectionUtil
  11. {
  12. internal static readonly FieldInfo DynamicMethodReturnType =
  13. typeof(DynamicMethod).GetField("returnType", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
  14. /// <summary>
  15. /// Sets a field on the target object, as gotten from <typeparamref name="T"/>.
  16. /// </summary>
  17. /// <typeparam name="T">the type to get the field from</typeparam>
  18. /// <typeparam name="U">the type of the field to set</typeparam>
  19. /// <param name="obj">the object instance</param>
  20. /// <param name="fieldName">the field to set</param>
  21. /// <param name="value">the value to set it to</param>
  22. /// <exception cref="MissingFieldException">if <paramref name="fieldName"/> does not exist on <typeparamref name="T"/></exception>
  23. /// <seealso cref="FieldAccessor{T, U}.Set(ref T, string, U)"/>
  24. public static void SetField<T, U>(this T obj, string fieldName, U value)
  25. => FieldAccessor<T, U>.Set(ref obj, fieldName, value);
  26. /// <summary>
  27. /// Gets the value of a field.
  28. /// </summary>
  29. /// <typeparam name="T">the type to get the field from</typeparam>
  30. /// <typeparam name="U">the type of the field (result casted)</typeparam>
  31. /// <param name="obj">the object instance to pull from</param>
  32. /// <param name="fieldName">the name of the field to read</param>
  33. /// <returns>the value of the field</returns>
  34. /// <exception cref="MissingFieldException">if <paramref name="fieldName"/> does not exist on <typeparamref name="T"/></exception>
  35. /// <seealso cref="FieldAccessor{T, U}.Get(ref T, string)"/>
  36. public static U GetField<U, T>(this T obj, string fieldName)
  37. => FieldAccessor<T, U>.Get(ref obj, fieldName);
  38. /// <summary>
  39. /// Sets a property on the target object, as gotten from <typeparamref name="T"/>.
  40. /// </summary>
  41. /// <typeparam name="T">the type to get the property from</typeparam>
  42. /// <typeparam name="U">the type of the property to set</typeparam>
  43. /// <param name="obj">the object instance</param>
  44. /// <param name="propertyName">the property to set</param>
  45. /// <param name="value">the value to set it to</param>
  46. /// <exception cref="MissingMemberException">if <paramref name="propertyName"/> does not exist on <typeparamref name="T"/></exception>
  47. /// <seealso cref="PropertyAccessor{T, U}.Set(ref T, string, U)"/>
  48. public static void SetProperty<T, U>(this T obj, string propertyName, U value)
  49. => PropertyAccessor<T, U>.Set(ref obj, propertyName, value);
  50. /// <summary>
  51. /// Gets a property on the target object, as gotten from <typeparamref name="T"/>.
  52. /// </summary>
  53. /// <typeparam name="T">the type to get the property from</typeparam>
  54. /// <typeparam name="U">the type of the property to get</typeparam>
  55. /// <param name="obj">the object instance</param>
  56. /// <param name="propertyName">the property to get</param>
  57. /// <returns>the value of the property</returns>
  58. /// <exception cref="MissingMemberException">if <paramref name="propertyName"/> does not exist on <typeparamref name="T"/></exception>
  59. /// <seealso cref="PropertyAccessor{T, U}.Get(ref T, string)"/>
  60. public static U GetProperty<U, T>(this T obj, string propertyName)
  61. => PropertyAccessor<T, U>.Get(ref obj, propertyName);
  62. /// <summary>
  63. /// Invokes a method from <typeparamref name="T"/> on an object.
  64. /// </summary>
  65. /// <typeparam name="U">the type that the method returns</typeparam>
  66. /// <typeparam name="T">the type to search for the method on</typeparam>
  67. /// <param name="obj">the object instance</param>
  68. /// <param name="methodName">the method's name</param>
  69. /// <param name="args">the method arguments</param>
  70. /// <returns>the return value</returns>
  71. /// <exception cref="MissingMethodException">if <paramref name="methodName"/> does not exist on <typeparamref name="T"/></exception>
  72. public static U InvokeMethod<U, T>(this T obj, string methodName, params object[] args)
  73. {
  74. var dynMethod = typeof(T).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
  75. if (dynMethod == null) throw new MissingMethodException($"Method {methodName} does not exist", nameof(methodName));
  76. return (U)dynMethod?.Invoke(obj, args);
  77. }
  78. /// <summary>
  79. /// Invokes a generic method from <typeparamref name="T"/> on an object.
  80. /// </summary>
  81. /// <typeparam name="U">the type that the method returns</typeparam>
  82. /// <typeparam name="T">the type to search for the method on</typeparam>
  83. /// <typeparam name="G">the generic type to invoke the method with</typeparam>
  84. /// <param name="obj">the object instance</param>
  85. /// <param name="methodName">the method's name</param>
  86. /// <param name="args">the method arguments</param>
  87. /// <returns>the return value</returns>
  88. /// <exception cref="T:System.MissingMethodException">if <paramref name="methodName" /> does not exist on <typeparamref name="T" /></exception>
  89. public static U InvokeGenericMethod<U, T, G>(this T obj, string methodName, params object[] args)
  90. {
  91. var dynMethod = typeof(T).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
  92. if (dynMethod == null) throw new MissingMethodException($"Method {methodName} does not exist", nameof(methodName));
  93. var genMethod = dynMethod.MakeGenericMethod(typeof(G));
  94. return (U)genMethod?.Invoke(obj, args);
  95. }
  96. /// <summary>
  97. /// Copies a component <paramref name="original"/> to a component of <paramref name="overridingType"/> on the destination <see cref="GameObject"/>.
  98. /// </summary>
  99. /// <param name="original">the original component</param>
  100. /// <param name="overridingType">the new component's type</param>
  101. /// <param name="destination">the destination GameObject</param>
  102. /// <param name="originalTypeOverride">overrides the source component type (for example, to a superclass)</param>
  103. /// <returns>the copied component</returns>
  104. public static Component CopyComponent(this Component original, Type overridingType, GameObject destination, Type originalTypeOverride = null)
  105. {
  106. var copy = destination.AddComponent(overridingType);
  107. var originalType = originalTypeOverride ?? original.GetType();
  108. Type type = originalType;
  109. while (type != typeof(MonoBehaviour))
  110. {
  111. CopyForType(type, original, copy);
  112. type = type?.BaseType;
  113. }
  114. return copy;
  115. }
  116. /// <summary>
  117. /// A generic version of <see cref="CopyComponent(Component, Type, GameObject, Type)"/>.
  118. /// </summary>
  119. /// <seealso cref="CopyComponent(Component, Type, GameObject, Type)"/>
  120. /// <typeparam name="T">the overriding type</typeparam>
  121. /// <param name="original">the original component</param>
  122. /// <param name="destination">the destination game object</param>
  123. /// <param name="originalTypeOverride">overrides the source component type (for example, to a superclass)</param>
  124. /// <returns>the copied component</returns>
  125. public static T CopyComponent<T>(this Component original, GameObject destination, Type originalTypeOverride = null)
  126. where T : Component
  127. {
  128. var copy = destination.AddComponent<T>();
  129. var originalType = originalTypeOverride ?? original.GetType();
  130. Type type = originalType;
  131. while (type != typeof(MonoBehaviour))
  132. {
  133. CopyForType(type, original, copy);
  134. type = type?.BaseType;
  135. }
  136. return copy;
  137. }
  138. /// <summary>
  139. /// Converts the property name to the one of the compiler-generated backing field.
  140. /// This can be used for the field-based reflection when you want to set the value of a get-only property
  141. /// </summary>
  142. /// <param name="propertyName">Name of the property</param>
  143. /// <returns>Name of the backing field</returns>
  144. /// <remarks>
  145. /// Only works for properties with compiler-generated backing fields.
  146. /// This is only a simple method and doesn't have any guarantees to work 100% of the time across different compilers/runtimes.
  147. /// See <a href="https://github.com/dotnet/roslyn/blob/1497e87d967c5b7797edb5f782131508607139e5/src/Compilers/CSharp/Portable/Symbols/Synthesized/GeneratedNames.cs#L24-L28">this link</a> for more info.
  148. /// </remarks>
  149. public static string ToCompilerGeneratedBackingField(string propertyName) => $"<{propertyName}>k__BackingField";
  150. private static void CopyForType(Type type, Component source, Component destination)
  151. {
  152. FieldInfo[] myObjectFields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
  153. foreach (FieldInfo fi in myObjectFields)
  154. {
  155. fi.SetValue(destination, fi.GetValue(source));
  156. }
  157. }
  158. }
  159. }