예제 #1
0
 public Statement(Block block, StatementTypes sType, Variable.Object var, Variable.Object val, string note) {
     gBlock = block;
     gAct = gBlock.Actuator;
     gStatementType = sType;
     //gArgs = args;
     gObjVar = var;
     gObjVal = val;
     gszNote = note;
 }
예제 #2
0
        //调用外部函数
        private void ExecuteCall() {
            string szVarType = "";
            if (gObjVar != null) szVarType = gObjVar.Type.ToString();
            string szVal = gObjVal.ToString().ToLower();

            //获取参数定义
            int nIndex = gAct.MemeryManager.GetIndex("{000}");
            if (nIndex < 0) throw new Exception("调用外部对象\"" + szVal + "\"之前未设置{000}参数数量!");
            Variable.Number num = (Variable.Number)gAct.MemeryManager[nIndex];

            bool bFound = false;
            foreach (System.Reflection.MethodInfo item in gAct.Library.GetType().GetMethods()) {

                //System.Windows.Forms.MessageBox.Show()

                // 获取所有的RecordAttributes特性   
                object[] records = item.GetCustomAttributes(typeof(dyk.Script.Code.Library.DykAttribute), false);

                if (records.Length > 0) {
                    dyk.Script.Code.Library.DykAttribute dAttr = (dyk.Script.Code.Library.DykAttribute)records[0];
                    System.Reflection.ParameterInfo[] dParameters = item.GetParameters();

                    if (szVal == item.Name.ToLower() && num.Value == dAttr.ArgCount) {
                        bFound = true;
                        switch (dAttr.AttributeType) {
                            case Library.AttributeTypes.Sub:
                                #region [=====执行方法=====]
                                if (szVarType != "") throw new Exception("外部调用对象\"" + szVal + "\"为方法,不具有返回值,请将返回变量设置为{}");

                                //添加执行参数
                                Variable.Object[] args = new Variable.Object[num.Value];
                                for (int i = 0; i < num.Value; i++) {
                                    string szVarName = "{" + (i + 1).ToString().PadLeft(3, '0') + "}";

                                    //读取参数指针
                                    int nVarIndex = gAct.MemeryManager.GetIndex(szVarName);
                                    if (nVarIndex < 0) throw new Exception("调用外部对象\"" + szVal + "\"的参数未设置完整!");
                                    Variable.Number numVal = (Variable.Number)gAct.MemeryManager[nVarIndex];

                                    //将变量对象赋值到参数
                                    string szParameterType = dParameters[i].ParameterType.ToString();
                                    Variable.Object objVar = gAct.MemeryManager[(int)numVal.Value];
                                    if (szParameterType == "dyk.Script.Code.Variable." + objVar.Type.ToString()) {
                                        args[i] = objVar;
                                    } else if (szParameterType == "dyk.Script.Code.Variable.Decimal") {
                                        args[i] = new Variable.Decimal(objVar.ToDecimal());
                                    } else if (szParameterType == "dyk.Script.Code.Variable.Number") {
                                        args[i] = new Variable.Number(objVar.ToNumber());
                                    } else if (szParameterType == "dyk.Script.Code.Variable.String") {
                                        args[i] = new Variable.String(objVar.ToString());
                                    } else {
                                        throw new Exception("调用外部对象\"" + szVal + "\"的参数不正确!");
                                    }
                                }
                                item.Invoke(gAct.Library, args);
                                break;
                            #endregion
                            case Library.AttributeTypes.Function:
                                #region [=====执行函数=====]

                                //添加执行参数
                                args = new Variable.Object[num.Value];
                                for (int i = 0; i < num.Value; i++) {
                                    string szVarName = "{" + (i + 1).ToString().PadLeft(3, '0') + "}";

                                    //读取参数指针
                                    int nVarIndex = gAct.MemeryManager.GetIndex(szVarName);
                                    if (nVarIndex < 0) throw new Exception("调用外部对象\"" + szVal + "\"的参数未设置完整!");
                                    Variable.Number numVal = (Variable.Number)gAct.MemeryManager[nVarIndex];

                                    //将变量对象赋值到参数
                                    //System.Windows.Forms.MessageBox.Show(item.GetParameters()[i].ParameterType.ToString());
                                    string szParameterType = dParameters[i].ParameterType.ToString();
                                    Variable.Object objVar = gAct.MemeryManager[(int)numVal.Value];
                                    if (szParameterType == "dyk.Script.Code.Variable." + objVar.Type.ToString()) {
                                        args[i] = objVar;
                                    } else if (szParameterType == "dyk.Script.Code.Variable.Decimal") {
                                        args[i] = new Variable.Decimal(objVar.ToDecimal());
                                    } else if (szParameterType == "dyk.Script.Code.Variable.Number") {
                                        args[i] = new Variable.Number(objVar.ToNumber());
                                    } else if (szParameterType == "dyk.Script.Code.Variable.String") {
                                        args[i] = new Variable.String(objVar.ToString());
                                    } else {
                                        throw new Exception("调用外部对象\"" + szVal + "\"的参数不正确!");
                                    }

                                }
                                object obj = item.Invoke(gAct.Library, args);

                                //Variable.Number dec = new Variable.Decimal(45);

                                //设置返回值
                                if (szVarType == "String") {
                                    Variable.String strVar = (Variable.String)gObjVar;
                                    strVar.Value = obj.ToString();
                                } else if (szVarType == "Decimal") {
                                    double res = 0;
                                    if (double.TryParse(obj.ToString(), out res)) {
                                        Variable.Decimal decVar = (Variable.Decimal)gObjVar;
                                        decVar.Value = res;
                                    } else {
                                        throw new Exception("类型不正确,无法强制转化为Decimal类型");
                                    }
                                } else if (szVarType == "Number") {
                                    long res = 0;
                                    if (long.TryParse(obj.ToString(), out res)) {
                                        Variable.Number numVar = (Variable.Number)gObjVar;
                                        numVar.Value = res;
                                    } else {
                                        throw new Exception("类型不正确,无法强制转化为Number类型");
                                    }
                                }

                                break;
                                #endregion
                        }

                    }
                    //MessageBox.Show(dAttr.AttributeType.ToString() + ":" + item.IsStatic + ":" + item.Name + "(" + item.GetParameters().Length + ")");
                }
            }

            if (!bFound) throw new Exception("未找到具有" + num.Value + "个参数定义的\"" + szVal + "\"外部调用对象");
        }