Exemple #1
0
        public object Call(ScriptObject[] parameters)
        {
            if (m_Count == 0)
            {
                throw new ScriptException("找不到函数 [" + MethodName + "]");
            }
            FunctionMethod methodInfo = null;

            if (m_Count == 1)
            {
                if (parameters.Length == m_Methods[0].ParameterType.Length)
                {
                    methodInfo = m_Methods[0];
                }
            }
            else
            {
                foreach (FunctionMethod method in m_Methods)
                {
                    if (Util.CanChangeType(parameters, method.ParameterType))
                    {
                        methodInfo = method;
                        break;
                    }
                }
            }
            if (methodInfo != null)
            {
                int      length = methodInfo.ParameterType.Length;
                object[] objs   = new object[length];
                for (int i = 0; i < length; i++)
                {
                    objs[i] = Util.ChangeType(parameters[i], methodInfo.ParameterType[i]);
                }
                return(methodInfo.Invoke(m_Object, objs));
            }
            else
            {
                foreach (FunctionMethod method in m_Methods)
                {
                    int length = method.ParameterType.Length;
                    if (method.Params && parameters.Length >= length - 1)
                    {
                        bool fit = true;
                        for (int i = 0; i < parameters.Length; ++i)
                        {
                            if (!Util.CanChangeType(parameters[i], i >= length - 1 ? method.ParamType : method.ParameterType[i]))
                            {
                                fit = false;
                                break;
                            }
                        }
                        if (fit)
                        {
                            object[] objs = new object[length];
                            for (int i = 0; i < length - 1; ++i)
                            {
                                objs[i] = Util.ChangeType(parameters[i], method.ParameterType[i]);
                            }
                            List <object> param = new List <object>();
                            for (int i = length - 1; i < parameters.Length; ++i)
                            {
                                param.Add(Util.ChangeType(parameters[i], method.ParamType));
                            }
                            objs[length - 1] = param.ToArray();
                            return(method.Invoke(m_Object, objs));
                        }
                    }
                }
                throw new ScriptException("找不到合适的函数 [" + MethodName + "]");
            }
        }
        public object Call(object obj, ScriptObject[] parameters)
        {
            if (m_Count == 0)
            {
                throw new ExecutionException(m_Script, "找不到函数 [" + MethodName + "]");
            }
            FunctionMethod methodInfo = null;

            if (m_Count == 1)
            {
                methodInfo = m_Methods[0];
                if (!methodInfo.IsValid)
                {
                    throw new ExecutionException(m_Script, "Type[" + m_Type.ToString() + "] 找不到合适的函数 [" + MethodName + "]");
                }
            }
            else
            {
                foreach (FunctionMethod method in m_Methods)
                {
                    if (method.IsValid && Util.CanChangeType(parameters, method.ParameterType))
                    {
                        methodInfo = method;
                        break;
                    }
                }
            }
            try {
                if (methodInfo != null && !methodInfo.Params)
                {
                    int      length = methodInfo.ParameterType.Length;
                    object[] objs   = methodInfo.Args;
                    for (int i = 0; i < length; i++)
                    {
                        objs[i] = Util.ChangeType(m_Script, parameters[i], methodInfo.ParameterType[i]);
                    }
                    return(methodInfo.Invoke(obj, m_Type));
                }
                else
                {
                    foreach (FunctionMethod method in m_Methods)
                    {
                        int length = method.ParameterType.Length;
                        if (method.Params && parameters.Length >= length - 1)
                        {
                            bool fit = true;
                            for (int i = 0; i < parameters.Length; ++i)
                            {
                                if (!Util.CanChangeType(parameters[i], i >= length - 1 ? method.ParamType : method.ParameterType[i]))
                                {
                                    fit = false;
                                    break;
                                }
                            }
                            if (fit)
                            {
                                object[] objs = method.Args;
                                for (int i = 0; i < length - 1; ++i)
                                {
                                    objs[i] = Util.ChangeType(m_Script, parameters[i], method.ParameterType[i]);
                                }
                                Array array = Array.CreateInstance(method.ParamType, parameters.Length - length + 1);
                                for (int i = length - 1; i < parameters.Length; ++i)
                                {
                                    array.SetValue(Util.ChangeType(m_Script, parameters[i], method.ParamType), i - length + 1);
                                }
                                objs[length - 1] = array;
                                return(method.Invoke(obj, m_Type));
                            }
                        }
                    }
                }
            } catch (System.Exception e) {
                throw new ExecutionException(m_Script, "Type[" + m_Type.ToString() + "] 调用函数出错 [" + MethodName + "] : " + e.ToString());
            }
            throw new ExecutionException(m_Script, "Type[" + m_Type.ToString() + "] 找不到合适的函数 [" + MethodName + "]");
        }