UnityMethod.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using Chronos.Reflection.Internal;
  5. using UnityEngine;
  6. using UnityObject = UnityEngine.Object;
  7. namespace Chronos.Reflection
  8. {
  9. [Serializable]
  10. public class UnityMethod : UnityMember, ISerializationCallbackReceiver
  11. {
  12. /// <summary>
  13. /// The underlying reflected method.
  14. /// </summary>
  15. public MethodInfo methodInfo { get; private set; }
  16. /// <summary>
  17. /// Whether the reflected method is an extension method.
  18. /// </summary>
  19. public bool isExtension { get; private set; }
  20. [SerializeField]
  21. private string[] _parameterTypes;
  22. private Type[] __parameterTypes;
  23. /// <summary>
  24. /// The types of the method's parameters.
  25. /// </summary>
  26. public Type[] parameterTypes
  27. {
  28. get { return __parameterTypes; }
  29. set { __parameterTypes = value; isReflected = false; }
  30. }
  31. public void OnAfterDeserialize()
  32. {
  33. if (_parameterTypes != null)
  34. {
  35. parameterTypes = _parameterTypes.Select(typeName => TypeSerializer.Deserialize(typeName)).ToArray();
  36. }
  37. }
  38. public void OnBeforeSerialize()
  39. {
  40. if (parameterTypes != null)
  41. {
  42. _parameterTypes = parameterTypes.Select(type => TypeSerializer.Serialize(type)).ToArray();
  43. }
  44. }
  45. #region Constructors
  46. public UnityMethod() { }
  47. public UnityMethod(string name) : base(name) { }
  48. public UnityMethod(string name, UnityObject target) : base(name, target) { }
  49. public UnityMethod(string component, string name) : base(component, name) { }
  50. public UnityMethod(string component, string name, UnityObject target) : base(component, name, target) { }
  51. public UnityMethod(string name, Type[] parameterTypes) : base(name) { this.parameterTypes = parameterTypes; }
  52. public UnityMethod(string name, Type[] parameterTypes, UnityObject target) : this(name, parameterTypes) { this.target = target; Reflect(); }
  53. public UnityMethod(string component, string name, Type[] parameterTypes) : base(component, name) { this.parameterTypes = parameterTypes; }
  54. public UnityMethod(string component, string name, Type[] parameterTypes, UnityObject target) : this(component, name, parameterTypes) { this.target = target; Reflect(); }
  55. #endregion
  56. /// <inheritdoc />
  57. public override void Reflect()
  58. {
  59. EnsureAssigned();
  60. EnsureTargeted();
  61. methodInfo = UnityMemberHelper.ReflectMethod(reflectionTarget, name, parameterTypes);
  62. isExtension = methodInfo.IsExtension();
  63. isReflected = true;
  64. }
  65. /// <summary>
  66. /// Invokes the method with any number of arguments of any type and returns its return value, or null if there isn't any (void).
  67. /// </summary>
  68. public object Invoke(params object[] parameters)
  69. {
  70. EnsureReflected();
  71. return UnityMemberHelper.InvokeMethod(reflectionTarget, methodInfo, isExtension, parameters);
  72. }
  73. /// <summary>
  74. /// Invokes the method with any number of arguments of any type and returns its return value casted to the specified type, or null if there isn't any (void).
  75. /// </summary>
  76. public T Invoke<T>(params object[] parameters)
  77. {
  78. return (T)Invoke(parameters);
  79. }
  80. /// <summary>
  81. /// The return type of the reflected method.
  82. /// </summary>
  83. public Type returnType
  84. {
  85. get
  86. {
  87. EnsureReflected();
  88. return methodInfo.ReturnType;
  89. }
  90. }
  91. public override bool Corresponds(UnityMember other)
  92. {
  93. return
  94. other is UnityMethod &&
  95. base.Corresponds(other) &&
  96. parameterTypes.SequenceEqual(((UnityMethod)other).parameterTypes);
  97. }
  98. }
  99. }