Esempio n. 1
0
        /// <summary>
        /// Execute this function with the specified number of parameters.
        /// </summary>

        public object Execute(params object[] pars)
        {
            if (mi == null)
            {
                return(null);
            }

            var parameters = this.parameters;

            if (pars == null && mParamCount != 0)
            {
                pars = new object[parameters.Length];
            }
            if (mParamCount == 1 && parameters[0].ParameterType == typeof(object[]))
            {
                pars = new object[] { pars }
            }
            ;

            try
            {
                if (mAutoCast)
                {
                    for (int i = 0; i < mParamCount; ++i)
                    {
                        var passed = pars[i].GetType();
                        if (mTypes[i] != passed)
                        {
                            pars[i] = Serialization.CastValue(pars[i], mTypes[i]);
                        }
                    }
                }
                return(mi.Invoke(obj, pars));
            }
            catch (Exception ex)
            {
                if (ex.GetType() == typeof(NullReferenceException))
                {
                    return(null);
                }

                var tryAgain = false;

                if (mParamCount == pars.Length)
                {
                    if (mTypes == null)
                    {
                        mTypes = new Type[mParamCount];
                        for (int i = 0; i < mParamCount; ++i)
                        {
                            mTypes[i] = parameters[i].ParameterType;
                        }
                    }

                    for (int i = 0; i < mParamCount; ++i)
                    {
                        var passed = (pars[i] != null) ? pars[i].GetType() : mTypes[i];

                        if (mTypes[i] != passed)
                        {
                            pars[i] = Serialization.CastValue(pars[i], mTypes[i]);
                            if (pars[i] != null)
                            {
                                tryAgain = true;
                            }
                        }
                    }
                }

                if (tryAgain)
                {
                    try
                    {
                        if (parameters.Length == 1 && parameters[0].ParameterType == typeof(object[]))
                        {
                            pars = new object[] { pars }
                        }
                        ;
                        var retVal = mi.Invoke(obj, pars);
                        mAutoCast = true;
                        return(retVal);
                    }
                    catch (Exception ex2) { ex = ex2; }
                }

                UnityTools.PrintException(ex, this, 0, mi.Name, pars);
                return(null);
            }
        }
    }