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.

143 lines
7.5 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. /// Copies a component <paramref name="original"/> to a component of <paramref name="overridingType"/> on the destination <see cref="GameObject"/>.
  80. /// </summary>
  81. /// <param name="original">the original component</param>
  82. /// <param name="overridingType">the new component's type</param>
  83. /// <param name="destination">the destination GameObject</param>
  84. /// <param name="originalTypeOverride">overrides the source component type (for example, to a superclass)</param>
  85. /// <returns>the copied component</returns>
  86. public static Component CopyComponent(this Component original, Type overridingType, GameObject destination, Type originalTypeOverride = null)
  87. {
  88. var copy = destination.AddComponent(overridingType);
  89. var originalType = originalTypeOverride ?? original.GetType();
  90. Type type = originalType;
  91. while (type != typeof(MonoBehaviour))
  92. {
  93. CopyForType(type, original, copy);
  94. type = type?.BaseType;
  95. }
  96. return copy;
  97. }
  98. /// <summary>
  99. /// A generic version of <see cref="CopyComponent(Component, Type, GameObject, Type)"/>.
  100. /// </summary>
  101. /// <seealso cref="CopyComponent(Component, Type, GameObject, Type)"/>
  102. /// <typeparam name="T">the overriding type</typeparam>
  103. /// <param name="original">the original component</param>
  104. /// <param name="destination">the destination game object</param>
  105. /// <param name="originalTypeOverride">overrides the source component type (for example, to a superclass)</param>
  106. /// <returns>the copied component</returns>
  107. public static T CopyComponent<T>(this Component original, GameObject destination, Type originalTypeOverride = null)
  108. where T : Component
  109. {
  110. var copy = destination.AddComponent<T>();
  111. var originalType = originalTypeOverride ?? original.GetType();
  112. Type type = originalType;
  113. while (type != typeof(MonoBehaviour))
  114. {
  115. CopyForType(type, original, copy);
  116. type = type?.BaseType;
  117. }
  118. return copy;
  119. }
  120. private static void CopyForType(Type type, Component source, Component destination)
  121. {
  122. FieldInfo[] myObjectFields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
  123. foreach (FieldInfo fi in myObjectFields)
  124. {
  125. fi.SetValue(destination, fi.GetValue(source));
  126. }
  127. }
  128. }
  129. }