Пример #1
0
        public static Expression GetCastExpression(Expression castexp, Expression argexp)
        {
            // take the value out of the var, if we've got a var
            if (argexp.Type == typeof(InputVar))
            {
                argexp = Expression.Field(argexp, "Value");
            }

            string castname = (castexp as ConstantExpression).Value as string;

            switch (castname.ToLower())
            {
            case "c:o":
                return(Expression.Convert(argexp, typeof(object)));

            case "c:b":
                if (argexp.Type == typeof(string))
                {
                    return(ExStrings.GetParseBoolExpression(argexp));
                }
                return(Expression.Convert(argexp, typeof(bool)));

            case "c:b2":
                return(Expression.Convert(argexp, typeof(bool[])));

            case "c:d":
                if (argexp.Type == typeof(string))
                {
                    return(ExStrings.GetParseDoubleExpression(argexp));
                }
                return(Expression.Convert(argexp, typeof(double)));

            case "c:d2":
                return(Expression.Convert(argexp, typeof(double[])));

            case "c:i":
                if (argexp.Type == typeof(string))
                {
                    return(ExStrings.GetParseIntExpression(argexp));
                }
                return(Expression.Convert(argexp, typeof(int)));

            case "c:i2":
                return(Expression.Convert(argexp, typeof(int[])));

            case "c:s":
                return(Expression.Call(typeof(ExCasts).GetTypeInfo().GetDeclaredMethod("ConvertToString"),
                                       Expression.Convert(argexp, typeof(object))));

            //return Expression.Convert(argexp, typeof(string));
            case "c:s2":
                return(Expression.Convert(argexp, typeof(string[])));

            default:
                // we didn't recognize the cast
                throw new Exception("Unrecognized cast '" + castname + "'");
            }
        }
Пример #2
0
        public static InputVar AddAssignAtRuntime(InputContext context, object o, object v)
        {
            InputVar var = GetVarAtRuntime(context, o, "add-assign");

            if (var.Value is string)
            {
                var.Value = ExStrings.Concat(var.Value, v);
                return(var);
            }
            else if (var.Value is double)
            {
                if (v is double)
                {
                    var.Value = ((double)var.Value) + ((double)v);
                }
                else if (v is int)
                {
                    var.Value = ((double)var.Value) + ((double)((int)v));
                }
                else
                {
                    throw new Exception("Tried to add-assign unsupported type '" + v.GetType() + "' to double var '" + var.Name + "'.");
                }
                return(var);
            }
            else if (var.Value is int)
            {
                if (v is double)
                {
                    var.Value = ((int)var.Value) + ((int)((double)v));
                }
                else if (v is int)
                {
                    var.Value = ((int)var.Value) + ((int)v);
                }
                else
                {
                    throw new Exception("Tried to add-assign unsupported type '" + v.GetType() + "' to int var '" + var.Name + "'.");
                }
                return(var);
            }
            throw new Exception("Tried to add-assign unsupported type '" + var.Value.GetType() + "'.");
        }