using System;
using System.Reflection;
using UnityEngine;
namespace Framework.Support {
/// Invokes protected or private methods on MonoBehaviors
///
/// Intended for unit testing
///
public static class MethodInvoker {
///
/// Calls the Inject() method on the specified MonoBehaviour-based object
///
///
/// MonoBehaviour on which Inject() will be invoked
///
///
/// Arguments that will be passed to the Inject() method
///
public static void CallInject(
this MonoBehaviour monoBehaviour, params object[] arguments
) {
MethodInfo injectMethodInfo = monoBehaviour.GetType().GetMethod(
"Inject", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public
);
injectMethodInfo.Invoke(monoBehaviour, arguments);
}
///
/// Calls the Awake() method on the specified MonoBehaviour-based object
///
///
/// MonoBehaviour on which Awake() will be invoked
///
public static void CallAwake(this MonoBehaviour monoBehaviour) {
MethodInfo awakeMethodInfo = monoBehaviour.GetType().GetMethod(
"Awake", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public
);
awakeMethodInfo.Invoke(monoBehaviour, null);
}
///
/// Calls the Update() method on the specified MonoBehaviour-based object
///
///
/// MonoBehaviour on which Update() will be invoked
///
public static void CallUpdate(this MonoBehaviour monoBehaviour) {
MethodInfo updateMethodInfo = monoBehaviour.GetType().GetMethod(
"Update", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public
);
updateMethodInfo.Invoke(monoBehaviour, null);
}
///
/// Calls the OnEnable() method on the specified MonoBehaviour-based object
///
///
/// MonoBehaviour on which OnEnable() will be invoked
///
public static void CallOnEnable(this MonoBehaviour monoBehaviour) {
MethodInfo onEnableMethodInfo = monoBehaviour.GetType().GetMethod(
"OnEnable", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public
);
onEnableMethodInfo.Invoke(monoBehaviour, null);
}
///
/// Calls the OnDestroy() method on the specified MonoBehaviour-based object
///
///
/// MonoBehaviour on which OnDestroy() will be invoked
///
public static void CallOnDestroy(this MonoBehaviour monoBehaviour) {
MethodInfo onDestroyMethodInfo = monoBehaviour.GetType().GetMethod(
"OnDestroy", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public
);
onDestroyMethodInfo.Invoke(monoBehaviour, null);
}
}
} // namespace Framework.Support