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)
         {
         }
     }
 }
Esempio n. 2
0
 private static Expression <Func <TEntity, bool> > GetContainExpression <TEntity>(ParameterExpression param, Lambda lambda, MemberExpression member)
 {
     if (lambda.Value is IEnumerable)
     {
         var convertValue = HandyConvert.ToList(lambda.Value as IEnumerable, lambda.PropertyType);
         var methodInfo   = typeof(List <>).MakeGenericType(lambda.PropertyType).GetMethod("Contains");
         var body         = Expression.Call(Expression.Constant(convertValue), methodInfo, member);
         return(Expression.Lambda <Func <TEntity, bool> >(body, param));
     }
     else
     {
         throw new ArgumentException("Contain 只适用与数组、集合等容器");
     }
 }
Esempio n. 3
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));
            }
        }
        public void TestToList()
        {
            Assert.IsNull(HandyConvert.ToList <string>(null));
            Assert.IsTrue(HandyConvert.ToList <string>(new string[] { "", "dd" }).Count == 2);
            Assert.IsTrue(HandyConvert.ToList <int>(new string[] { "456", "123" }).Count == 2);

            Assert.IsNotNull(HandyConvert.ToList(new string[] { "", "dd" }, typeof(string)) as List <string>);
            Assert.IsNotNull(HandyConvert.ToList(new HandyConvertTest_A[]
            {
                new HandyConvertTest_A
                {
                    a = "aa"
                },
                new HandyConvertTest_A
                {
                    a = "bb"
                }
            }, typeof(HandyConvertTest_A)) as List <dynamic>);
        }