/// <summary> /// Attempt to call a method on the managed instance with the specified name. /// Any exceptions thrown as a result of locating or calling the method will be caught silently. /// This works in a similar was as <see cref="UnityEngine.GameObject.SendMessage(string)"/> where the target method name is specified. /// Any number of arguments may be specified but the target method must expect the arguments. /// </summary> /// <param name="method">The name of the method to call</param> /// <param name="callConvention">The method calling convention</param> /// <param name="arguments">The arguments passed to the method</param> /// <returns>The value returned from the target method or null if the target method does not return a value</returns> public object SafeCall(string method, ProxyCallConvention callConvention, params object[] arguments) { try { // Make sure the object has not already been disposed CheckDisposed(); // Catch any exceptions return(Call(method, callConvention)); } catch { // Exception - Maybe cause by the target method return(null); } }
/// <summary> /// Attempt to call a method on the managed instance with the specified name and arguments. /// This works in a similar was as <see cref="UnityEngine.GameObject.SendMessage(string)"/> where the method name is specified. /// Any number of arguments may be specified but the target method must expect the arguments. /// </summary> /// <param name="methodName">The name of the method to call</param> /// <param name="callConvention">The method calling convention</param> /// <param name="arguments">The arguments passed to the method</param> /// <returns>The value returned from the target method or null if the target method does not return a value</returns> /// <exception cref="ObjectDisposedException">The proxy has already been disposed</exception> /// <exception cref="TargetException">The target method could not be found on the managed type</exception> public object Call(string methodName, ProxyCallConvention callConvention, params object[] arguments) { // Make sure the object has not already been disposed CheckDisposed(); // Find the method MethodInfo method = scriptType.FindCachedMethod(methodName, false); // Check for error if (method == null) { throw new TargetException(string.Format("Type '{0}' does not define a method called '{1}'", ScriptType, methodName)); } // Call the method object result = method.Invoke(instance, arguments); // Check for coroutine if ((result is IEnumerator) && (callConvention == ProxyCallConvention.Any || callConvention == ProxyCallConvention.UnityCoroutine) == true) { // Get the coroutine method IEnumerator routine = result as IEnumerator; // Check if the calling object is a mono behaviour if (IsMonoBehaviour == true) { // Get the proxy as a mono behaviour MonoBehaviour mono = GetInstanceAs <MonoBehaviour>(false); // Register the coroutine with the behaviour so that it will be called after yield mono.StartCoroutine(routine); } } // Get the return value return(result); }