Ejemplo n.º 1
0
 public static Expression ConvertToBool(this Expression E)
 {
     if (E.Type == typeof(bool))
     {
         return(E);
     }
     else if (NumericType.Contains(E.Type))
     {
         return(Expression.GreaterThan(E, Expression.Constant(0, typeof(int))));
     }
     else if (E.Type == typeof(string))
     {
         return(Expression.NotEqual(Expression.Constant("", typeof(string)), E));
     }
     else
     {
         return(Expression.NotEqual(E, Expression.Constant(null)));
     }
 }
Ejemplo n.º 2
0
        public static Expression ConvertToNumber(this Expression E, Type Favorite = null)
        {
            if (E.GetType() == typeof(ConstantExpression))
            {
                return(((ConstantExpression)E).ConvertTo(Favorite ?? typeof(double)));
            }
            if (NumericType.Contains(E.Type))
            {
                if (E.Type == Favorite)
                {
                    return(E);
                }
                else if (Favorite != null)
                {
                    return(Expression.Convert(E, Favorite));
                }
            }

            if (E.GetType() == typeof(VarAutExpression))
            {
                return(((VarAutExpression)E).Getter(Favorite ?? typeof(double)));
            }

            if (NumericType.Contains(E.Type))
            {
                return((Favorite != null && E.Type != Favorite) ? Expression.Convert(E, Favorite) : E);
            }
            else if (E.Type == typeof(string))
            {
                return(Expression.Call(Numeric_Parse(Favorite ?? typeof(double)), E, InvariantCultureFieldAccess));
            }
            else if (E.Type == typeof(bool))
            {
                return(Expression.IfThenElse(E, Expression.Constant(1, Favorite ?? typeof(int)), Expression.Constant(0, Favorite ?? typeof(int))));
            }

            else
            {
                return(Expression.Convert(Expression.Call(Convert_ChangeType, Expression.Convert(E, typeof(object)), Expression.Constant(Favorite, typeof(Type))), Favorite));
            }
        }
Ejemplo n.º 3
0
        public static Expression ConvertTo(this Expression E, Type T)
        {
            if (E.GetType() == typeof(ConstantExpression))
            {
                return(((ConstantExpression)E).ConvertTo(T));
            }
            if (E.GetType() == typeof(VarAutExpression))
            {
                return(((VarAutExpression)E).Getter(T));
            }

            if (E.Type == T)
            {
                return(E);
            }
            else if (T == typeof(string))
            {
                return(E.ConvertToString());
            }
            else if (NumericType.Contains(T))
            {
                return(E.ConvertToNumber(T));
            }
            else if (T == typeof(bool))
            {
                return(E.ConvertToBool());
            }
            else if (T == typeof(object))
            {
                return(Expression.Convert(E, T));
            }
            else
            {
                return(Expression.Convert(Expression.Call(Convert_ChangeType, Expression.Convert(E, typeof(object)), Expression.Constant(T, typeof(Type))), T));
            }
        }
        public static Expression ConvertTo(this ConstantExpression E, Type T)
        {
            if (E.Type == T)
            {
                return(E);
            }
            if (T == typeof(string)) //Convert to string ?
            {
                return(Expression.Constant(Convert.ToString(E.Value, CultureInfo.InvariantCulture), T));
            }

            /*else if (NumericType.Contains(T)) //Convert to number ?
             *  return Expression.Constant(Convert.ToDouble(E.Value, CultureInfo.InvariantCulture), T);*/
            if (T == typeof(bool))
            {
                return(Expression.Constant(E.Type == typeof(string) ? (string)E.Value != "" : NumericType.Contains(E.Value) ? (dynamic)E.Value > 0 : E.Value != null, T));
            }
            if (T == typeof(object))
            {
                return(Expression.Convert(E, T));
            }
            else
            {
                try  //It's an IConvertible ?
                {
                    return(Expression.Constant(Convert.ChangeType(E.Value, T), T));
                }
                catch //It's not, return default
                {
                    return(Expression.Constant(T.DefaultValue(), T));
                }
            }
        }