예제 #1
0
 protected override IEnumerable <Expression> CreateExpressionCore(CreatePropertyValidatorInput input)
 {
     if (input.PropertyInfo.PropertyType.GetInterfaces().Any(x => x == typeof(IEnumerable <T>)))
     {
         yield return(CreateValidateExpression(input, Func()));
     }
 }
예제 #2
0
            protected Expression CreateValidateExpression(
                CreatePropertyValidatorInput input,
                Expression <Func <string, TValue, ValidateResult> > validateFuncExpression)
            {
                var propertyInfo = input.PropertyInfo;
                var isOkProperty = typeof(ValidateResult).GetProperty(nameof(ValidateResult.IsOk));

                Debug.Assert(isOkProperty != null, nameof(isOkProperty) + " != null");

                var convertedExp = Expression.Convert(input.InputExpression, input.InputType);
                var propExp      = Expression.Property(convertedExp, propertyInfo);
                var nameExp      = Expression.Constant(propertyInfo.Name);

                var requiredMethodExp = Expression.Invoke(
                    validateFuncExpression,
                    nameExp,
                    propExp);
                var assignExp             = Expression.Assign(input.ResultExpression, requiredMethodExp);
                var resultIsOkPropertyExp = Expression.Property(input.ResultExpression, isOkProperty);
                var conditionExp          = Expression.IsFalse(resultIsOkPropertyExp);
                var ifThenExp             =
                    Expression.IfThen(conditionExp,
                                      Expression.Return(input.ReturnLabel, input.ResultExpression));
                var re = Expression.Block(
                    new[] { input.ResultExpression },
                    assignExp,
                    ifThenExp);

                return(re);
            }
 public virtual IEnumerable <Expression> CreateExpression(CreatePropertyValidatorInput input)
 {
     if (input.PropertyInfo.PropertyType != typeof(T))
     {
         return(Enumerable.Empty <Expression>());
     }
     return(CreateExpressionCore(input));
 }
예제 #4
0
 protected override IEnumerable<Expression> CreateExpressionCore(CreatePropertyValidatorInput input)
 {
     if (input.PropertyInfo.GetCustomAttribute<MinLengthAttribute>() is { } v)
     {
         Expression<Func<string, bool>> checkbodyExp = x => string.IsNullOrEmpty(x) || x.Length < v.Length;
         Expression<Func<string, string>> ErrMessageExp = (s) => $"Length of {s} should be great than {v.Length}";
         yield return CreateValidateExpression(input, ExpressionHelper.CreateCheckerExpression(typeof(string), checkbodyExp, ErrMessageExp));
     }
 }
 protected override IEnumerable <Expression> CreateExpressionCore(CreatePropertyValidatorInput input)
 {
     if (input.PropertyInfo.GetCustomAttribute <MinLengthAttribute>() is { } p)
     {
         Expression <Func <int, bool> >      checkbodyExp  = x => x.ToString().Length < p.Length;
         Expression <Func <string, string> > ErrMessageExp = (s) => $"Value if {s} should be great then {p.Length}";
         yield return(CreateValidateExpression(input, ExpressionHelper.CreateCheckerExpression(typeof(int), checkbodyExp, ErrMessageExp)));
     }
 }
예제 #6
0
 protected override IEnumerable <Expression> CreateExpressionCore(CreatePropertyValidatorInput input)
 {
     if (input.PropertyInfo.GetCustomAttribute <RangeAttribute>() is RangeAttribute p)
     {
         Expression <Func <int, bool> >      checkbodyExp  = x => x <(int)p.Minimum || x> (int) p.Maximum;
         Expression <Func <string, string> > ErrMessageExp = (s) => $"{s} must range in {p.Minimum} and {p.Maximum}";
         yield return(CreateValidateExpression(input, ExpressionHelper.CreateCheckerExpression(typeof(int), checkbodyExp, ErrMessageExp)));
     }
 }
 protected override IEnumerable <Expression> CreateExpressionCore(CreatePropertyValidatorInput input)
 {
     if (input.PropertyInfo.GetCustomAttribute <RequiredAttribute>() is not null)
     {
         Expression <Func <string, bool> >   checkbodyExp  = x => string.IsNullOrEmpty(x);
         Expression <Func <string, string> > ErrMessageExp = x => $"missing {x}";
         yield return(CreateValidateExpression(input, ExpressionHelper.CreateCheckerExpression(typeof(string), checkbodyExp, ErrMessageExp)));
     }
 }
예제 #8
0
            protected override IEnumerable <Expression> CreateExpressionCore(CreatePropertyValidatorInput input)
            {
                var propertyInfo = input.PropertyInfo;

                if (propertyInfo.GetCustomAttribute <RequiredAttribute>() != null)
                {
                    yield return(CreateValidateExpression(input, CreateValidateStringRequiredExp()));
                }
            }
예제 #9
0
 private IEnumerable <Expression> GetExpressions(CreatePropertyValidatorInput input)
 {
     {
         var        argType      = input.PropertyInfo.PropertyType.GetGenericArguments().First();
         var        inputType    = typeof(IEnumerable <>).MakeGenericType(argType);
         Expression checkbodyExp = CreateCheckBodyExpression(argType);
         Expression <Func <string, string> > ErrMessageExp = x => $"{x} must has any";
         var r = (Expression)(this.GetType().GetMethod(nameof(CreateValidateExpression)).MakeGenericMethod(input.PropertyInfo.PropertyType.GetGenericArguments()).Invoke(this, new object[] { input, ExpressionHelper.CreateCheckerExpression(typeof(IEnumerable <>).MakeGenericType(argType), checkbodyExp, ErrMessageExp) }));
         yield return(r);
     }
 }
예제 #10
0
            protected override IEnumerable <Expression> CreateExpressionCore(CreatePropertyValidatorInput input)
            {
                var propertyInfo       = input.PropertyInfo;
                var minlengthAttribute = propertyInfo.GetCustomAttribute <MinLengthAttribute>();

                if (minlengthAttribute != null)
                {
                    yield return(CreateValidateExpression(input,
                                                          CreateValidateStringMinLengthExp(minlengthAttribute.Length)));
                }
            }
예제 #11
0
 public virtual IEnumerable <Expression> CreateExpression(CreatePropertyValidatorInput input)
 {
     if (input.PropertyInfo.PropertyType == typeof(string))
     {
         return(Enumerable.Empty <Expression>());
     }
     if (input.PropertyInfo.PropertyType.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition().IsAssignableFrom(typeof(IEnumerable <>))))
     {
         return(GetExpressions(input));
     }
     return(Enumerable.Empty <Expression>());
 }
        protected Expression CreateValidateExpression(CreatePropertyValidatorInput input, Expression validateFuncExpression)
        {
            var propertyInfo      = input.PropertyInfo;
            var isOkProperty      = typeof(ValidateResult).GetProperty(nameof(ValidateResult.IsOK));
            var convertExp        = Expression.Convert(input.InputExpression, input.InputType);
            var propExp           = Expression.Property(convertExp, propertyInfo);
            var nameContestExp    = Expression.Constant(propertyInfo.Name, typeof(string));
            var requiredMethodExp = Expression.Invoke(validateFuncExpression, nameContestExp, propExp);
            var assignExp         = Expression.Assign(input.ResultExpression, requiredMethodExp);
            var resultIsOkExp     = Expression.Property(input.ResultExpression, isOkProperty);
            var ifthenExp         = Expression.IfThen(Expression.IsFalse(resultIsOkExp), Expression.Return(input.ReturnLabel, input.ResultExpression));

            return(Expression.Block(assignExp, ifthenExp));
        }
 protected abstract IEnumerable <Expression> CreateExpressionCore(CreatePropertyValidatorInput input);