Ejemplo n.º 1
0
        private void eval_AdditionalFunctionEventHandler(object sender, AdditionalFunctionEventArgs e)
        {
            object[] parameters = e.GetParameters();
            object   value;

            //检查是否在默认的函数库中
            switch (e.Name)
            {
            case "GetProperty":
                break;

            case "Get":
                if (Context == null)
                {
                    throw new ApplicationException("执行函数失败 上下文为空");
                }

                if (Context == null || parameters == null || parameters.Length < 1 || string.IsNullOrEmpty(parameters[0] as string))
                {
                    throw new ApplicationException("执行函数失败 入参输入为空");
                }
                if (!Context.TryGetValue(parameters[0] as string, out value))
                {
                    throw new ApplicationException("执行函数失败 获取上下文时 没有键为" + parameters[0] + "的值");
                }
                e.ReturnValue = value;
                return;

            default:
                string className = null;
                //检查是否在当前应用的函数库中
                if (DynStringResolver.MethodDict.TryGetValue(e.Name, out className))
                {
                    string methodName = e.Name;

                    Dictionary <string, object> dict = new Dictionary <string, object>();
                    DynClass function = DynTypeManager.GetFunction(className);

                    if (function == null)
                    {
                        throw new ApplicationException("执行函数失败 没有主键为" + className + "的函数类 请查看初始化");
                    }
                    DynMethod method = function.GetMethod(methodName);

                    DynParameter[] dynParameters = method.GetParameters();

                    for (int i = 0; i < dynParameters.Length; i++)
                    {
                        value = parameters.Length > i ? parameters[i] : null;

                        switch (dynParameters[i].CollectionType)
                        {
                        case CollectionType.None:
                            value = DynStringResolver.GetTrueType(value, dynParameters[i].DynType);
                            break;

                        case CollectionType.List:
                            IList valueList = value as IList;
                            for (int j = 0; j < valueList.Count; j++)
                            {
                                valueList[j] = DynStringResolver.GetTrueType(valueList[j], dynParameters[i].DynType);
                            }
                            break;

                        case CollectionType.Set:
                        case CollectionType.Map:
                            break;

                        default:
                            break;
                        }

                        dict.Add(dynParameters[i].Name, value);
                    }

                    e.ReturnValue = DynTypeManager.MethodHandler(null, className + "_" + methodName, dict);
                }
                else
                {
                    throw new ApplicationException("无此函数:" + e.Name);
                }
                break;
            }
        }
Ejemplo n.º 2
0
        public object GetDefaultValue()
        {
            DynObject dynDefaultValue = null;

            if (ContainsAttribute("DefaultValue"))
            {
                dynDefaultValue = GetAttribute("DefaultValue");
            }

            bool isHaveDefaultValue = false;

            if (dynDefaultValue != null && !string.IsNullOrEmpty(dynDefaultValue["Value"] as string))
            {
                string defaultType  = dynDefaultValue["Type"] as string;
                string defaultValue = dynDefaultValue["Value"] as string;
                switch (defaultType)
                {
                case "Direct":
                    object trueValue = null;
                    if (TryPasentValue(defaultValue, this, ref trueValue))
                    {
                        isHaveDefaultValue = true;
                        return(trueValue);
                    }
                    break;

                case "Compute":
                    return(DynStringResolver.Resolve(defaultValue));

                default:
                    break;
                }
            }

            if (!isHaveDefaultValue)
            {
                switch (CollectionType)
                {
                case CollectionType.None:
                    if (IsNotNull)
                    {
                        switch (DynType)
                        {
                        case DynType.Void:
                        case DynType.Bool:
                            return(false);

                        case DynType.Byte:
                            return(null);

                        case DynType.String:
                            return("");

                        case DynType.Struct:
                            return(null);

                        case DynType.DateTime:
                            return(DateTime.Now);

                        case DynType.Double:
                            return(0.0);

                        case DynType.Decimal:
                            return((decimal)0.0);

                        case DynType.I16:
                            return((short)0);

                        case DynType.I32:
                            return((int)0);

                        case DynType.I64:
                            return((long)0);

                        default:
                            break;
                        }
                    }
                    else
                    {
                        return(null);;
                    }

                    break;

                case CollectionType.List:
                    switch (DynType)
                    {
                    case DynType.Void:
                    case DynType.Bool:
                        return(new List <bool>());

                    case DynType.Byte:
                        return(new List <Byte>());

                    case DynType.String:
                        return(new List <String>());

                    case DynType.Struct:
                        return(new List <DynObject>());

                    case DynType.Double:
                        return(new List <Double>());

                    case DynType.Decimal:
                        return(new List <Decimal>());

                    case DynType.I16:
                        return(new List <Int16>());

                    case DynType.I32:
                        return(new List <Int32>());

                    case DynType.I64:
                        return(new List <Int64>());

                    case DynType.DateTime:
                        return(new List <DateTime>());

                    default:
                        break;
                    }

                    break;

                case CollectionType.Map:
                    return(new Dictionary <string, object>());

                case CollectionType.Set:
                    throw new ApplicationException("暂不支持Set集合类型");
                }
            }
            return(null);
        }