Пример #1
0
    //(text haystack, number at)
    public IDPacket EXTERNAL(string MethodName, IDPacket[] PARAMS)
    {
        IDPacket   ret   = null;
        MethodInfo minfo = this.GetType().GetMethod(MethodName);

        try {
            int           PCNT       = 0;
            List <object> parameters = new List <object>();
            foreach (ParameterInfo pinfo in minfo.GetParameters())
            {
                IDPacket    PARAM = PARAMS[PCNT++];
                System.Type ptype = pinfo.ParameterType;
                if (ptype == typeof(int) || ptype == typeof(double) || ptype == typeof(short) || ptype == typeof(long))
                {
                    if (PARAM.Type == IdentityType.Number)
                    {
                        double d;
                        TryGetNumber(PARAM, out d);
                        parameters.Add(d);
                    }
                    else
                    {
                        Error(String.Format("Type mismatch in external function call, expecting {0} in parameter {1} got {2}",
                                            ptype, PCNT, PARAM.Type));
                    }
                }
                else if (ptype == typeof(string))
                {
                    if (PARAM.Type == IdentityType.Number)
                    {
                        string s;
                        TryGetText(PARAM, out s);
                        parameters.Add(s);
                    }
                    else
                    {
                        Error(String.Format("Type mismatch in external function call, expecting {0} in parameter {1} got {2}",
                                            ptype, PCNT, PARAM.Type));
                    }
                }
                else if (ptype == typeof(bool))
                {
                    if (PARAM.Type == IdentityType.Boolean)
                    {
                        bool b;
                        TryGetBoolean(PARAM, out b);
                        parameters.Add(b);
                    }
                    else
                    {
                        Error(String.Format("Type mismatch in external function call, expecting {0} in parameter {1} got {2}",
                                            ptype, PCNT, PARAM.Type));
                    }
                }
                else
                {
                    Error(String.Format("The external function {0} cannot be called from the YSInterpreter", minfo.Name));
                }
            }
            object      returnValue = minfo.Invoke(minfo, parameters.ToArray());
            System.Type rtype       = minfo.ReturnType;
            if (rtype == typeof(int) || rtype == typeof(double) || rtype == typeof(short) || rtype == typeof(long))
            {
                ret = IDPacket.CreateReturnPacket(IdentityType.Number);
                PutNumber(ret, (double)returnValue);
            }
            else if (rtype == typeof(string))
            {
                ret = IDPacket.CreateReturnPacket(IdentityType.Text);
                PutText(ret, (string)returnValue);
            }
            else if (rtype == typeof(bool))
            {
                ret = IDPacket.CreateReturnPacket(IdentityType.Boolean);
                PutBoolean(ret, (bool)returnValue);
            }
            else
            {
                Debug(String.Format("[!] The external function {0}'s returning value of type {1} cannot be used by YSInterpreter",
                                    minfo.Name, minfo.ReturnType));
            }
        } catch (TargetInvocationException) {
            Error("Failed to invoke the external method");
            return(null);
        } catch (Exception) {
            Error("Failed to invoke the external method");
            return(null);
        }
        return(ret);
    }