예제 #1
0
 public void TestConvertTo()
 {
     {
         try
         {
             HandyConvert.ConvertTo <int>(null);
             Assert.IsFalse(true);
         }
         catch (InvalidCastException)
         {
         }
     }
     {
         HandyConvert.ConvertTo <int>("32");
         HandyConvert.ConvertTo <int>(32.5);
         HandyConvert.ConvertTo <int?>(32.5);
         Assert.IsTrue(HandyConvert.ConvertTo <bool>(123));
         Assert.IsTrue(HandyConvert.ConvertTo <bool>(-123));
         Assert.IsFalse(HandyConvert.ConvertTo <bool>(0));
     }
     {
         try
         {
             HandyConvert.ConvertTo <bool>("1");
             Assert.IsFalse(true);
         }
         catch (FormatException)
         {
         }
     }
 }
예제 #2
0
        //public static Expression<Func<TEntity, bool>> ToExpression<TEntity>(ParameterExpression param, params Lambda[] lambdas)
        //{

        //}

        private static Expression <Func <TEntity, bool> > GetExpression <TEntity>(ExpressionType expressionType, ParameterExpression param, MemberExpression member, dynamic value, Type propertyType)
        {
            var convertValue = HandyConvert.ConvertTo(value, propertyType);

            if (propertyType.IsNullable())
            {
                var binary = Expression.MakeBinary(expressionType, member, Expression.Convert(Expression.Constant(convertValue), propertyType));
                return(Expression.Lambda <Func <TEntity, bool> >(binary, param));
            }
            else
            {
                var binary = Expression.MakeBinary(expressionType, member, Expression.Constant(convertValue));
                return(Expression.Lambda <Func <TEntity, bool> >(binary, param));
            }
        }