Ejemplo n.º 1
0
        /// <summary>
        ///     方法调用
        /// </summary>
        /// <param name="stement"></param>
        /// <returns></returns>
        private ScriptValue ExcuteCall(TokenFunctionCallStement stement)
        {
            var first = stement.CallChain.First();

            var paremeters = new List <ScriptValue>();

            foreach (var value in stement.Parameters)
            {
                var scriptVar = ReturnValue(value);
                paremeters.Add(scriptVar);
            }

            if (_functions.ContainsKey(first))
            {
                return(ExcuteFunction(_functions[first], paremeters));
            }

            {
                var argTypes  = new List <Type>();
                var argValues = new List <object>();

                foreach (var paremeter in paremeters)
                {
                    var dataType = ScriptType.GetType(paremeter.DataType);
                    argTypes.Add(dataType);
                    argValues.Add(paremeter.Value);
                }

                //调用.Net框架中的方法
                //从程序集中找出名称和参数定义相同的方法
                var methods = AppDomain.CurrentDomain.GetAssemblies()
                              .SelectMany(it => it.DefinedTypes.Where(it1 => it1.Name == first)
                                          .Select(t => t.GetMethod(stement.CallChain.Last(), argTypes.ToArray()))
                                          .Where(m => m != null)
                                          ).ToList();
                if (!methods.Any())
                {
                    throw new Exception("没有找到对应的方法");
                }
                //   if(methods.Count()>1) throw new Exception("找到多个方法,无法确定调用哪个");
                var method   = methods.First();
                var retValue = new ScriptValue();
                var value    = method.Invoke(null, argValues.ToArray());
                if (method.ReturnType == typeof(void))
                {
                    return(null);
                }
                return(new ScriptValue
                {
                    Value = value
                });
            }
        }