Пример #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 + "'");
            }
        }