Exemple #1
0
        /// <summary>
        /// Invokes a method on all callbacks of a specific invocation context. If the method is static, then only a
        /// single call is made. If it's an instance method, then it is invoked on all instances.
        /// </summary>
        /// <param name="invocationContext">The invocation context.</param>
        /// <returns>True if the method was invoked successfully on all valid targets.</returns>
        private bool InvokeMethod(InvocationContext invocationContext)
        {
            var method = invocationContext.MethodInfo;
            var formalParametersInfo = method.GetParameters();
            var parameterObjects     = new object[formalParametersInfo.Length];

            for (var i = 0; i < formalParametersInfo.Length; i++)
            {
                if (!parameterProvider.ContainsParameter(formalParametersInfo[i]))
                {
                    Debug.LogError($"Failed to find parameter {formalParametersInfo[i].Name} while invoking {method.Name}");
                    return(false);
                }
                parameterObjects[i] = parameterProvider.GetParameterValue(formalParametersInfo[i]);
            }

            if (method.IsStatic)
            {
                Debug.Log($"About to invoke {method.Name}");
                try
                {
                    method.Invoke(null, parameterObjects.ToArray());
                }
                catch (Exception e)
                {
                    Debug.LogError($"Failed to invoke static method {method.Name}. {e}");
                    return(false);
                }

                return(true);
            }
            else
            {
                Debug.Log($"About to invoke {method.Name} on all instances");
                var allSucceeded = true;
                foreach (var obj in this.instanceResolver.GetObjectsOfType(invocationContext.Type))
                {
                    try
                    {
                        method.Invoke(obj, parameterObjects.ToArray());
                    }
                    catch (Exception e)
                    {
                        Debug.LogError($"Failed to invoke method {method.Name}. {e} on {obj}");
                        allSucceeded = false;
                        continue;
                    }
                }

                return(allSucceeded);
            }
        }