protected override IEnumerable <Expression> CreateExpressionCore(CreatePropertyValidatorInput input, Type iEnumerable) { ParameterExpression valueExp = Expression.Parameter(iEnumerable, "value"); TypeBinaryExpression typeIsExp = Expression.TypeIs(valueExp, typeof(ICollection)); UnaryExpression falseExp = Expression.IsFalse(typeIsExp); Type funcType = Expression.GetFuncType(iEnumerable, typeof(bool)); LambdaExpression body = Expression.Lambda(funcType, falseExp, valueExp); yield return(ExpressionHelp.CreateValidateExpression(input, ExpressionHelp.CreateCheckExpression(iEnumerable, body, toArryOrListErrorExp ) )); }
/// <summary> /// 值不能等于null验证 /// </summary> /// <param name="input"></param> /// <returns></returns> private Expression RequiredValidator(CreatePropertyValidatorInput input) { var requiredAttribute = input.InputProperty.GetCustomAttribute <RequiredAttribute>(); if (requiredAttribute == null) { return(Expression.Empty()); } Expression <Func <int?, bool> > checkBoxExp = value => value == null; return(ExpressionHelp.CreateValidateExpression(input, ExpressionHelp.CreateCheckExpression(typeof(int?), checkBoxExp, notNullErrorExp ))); }
protected override IEnumerable <Expression> CreateExpressionCore(CreatePropertyValidatorInput input) { var required = input.InputProperty.GetCustomAttribute <RequiredAttribute>(); if (required == null) { yield break; } Expression <Func <string, bool> > checkBoxExp = value => string.IsNullOrEmpty(value); yield return(ExpressionHelp.CreateValidateExpression(input, ExpressionHelp.CreateCheckExpression(typeof(string), checkBoxExp, requiredErrorExp ))); }
protected override IEnumerable <Expression> CreateExpressionCore(CreatePropertyValidatorInput input, Type iEnumerable) { ParameterExpression valueExp = Expression.Parameter(iEnumerable); BinaryExpression leftExp = Expression.Equal(valueExp, Expression.Constant(null)); MethodCallExpression anyExp = Expression.Call(typeof(Enumerable), nameof(Enumerable.Any), new Type[] { @iEnumerable.GenericTypeArguments[0] }, valueExp); Expression rightExp = Expression.IsFalse(anyExp); BinaryExpression testExp = Expression.OrElse(leftExp, rightExp); Type funcType = Expression.GetFuncType(iEnumerable, typeof(bool)); var checkBoxExp = Expression.Lambda(funcType, testExp, valueExp); yield return(ExpressionHelp.CreateValidateExpression(input, ExpressionHelp.CreateCheckExpression(iEnumerable, checkBoxExp, anyErrorExp ))); }
/// <summary> /// 指定范围验证 /// </summary> /// <param name="input"></param> /// <returns></returns> private Expression RangeValidator(CreatePropertyValidatorInput input) { var rangeAttribute = input.InputProperty.GetCustomAttribute <RangeAttribute>(); if (rangeAttribute == null) { return(Expression.Empty()); } int min = (int)rangeAttribute.Minimum; int max = (int)rangeAttribute.Maximum; Expression <Func <int, bool> > checkBoxExp = value => value <min || value> max; return(ExpressionHelp.CreateValidateExpression(input, ExpressionHelp.CreateCheckExpression(typeof(int), checkBoxExp, GetErrorExp(min, max) ))); }
/// <summary> /// 不能大于指定最大值验证 /// </summary> /// <param name="input"></param> /// <returns></returns> private Expression MaxLengthValidator(CreatePropertyValidatorInput input) { var maxLengthAttribute = input.InputProperty.GetCustomAttribute <MaxLengthAttribute>(); if (maxLengthAttribute == null) { return(Expression.Empty()); } var maxLength = maxLengthAttribute.Length; Expression <Func <string, bool> > checkBoxExp = value => string.IsNullOrEmpty(value) || value.Length > maxLength; return(ExpressionHelp.CreateValidateExpression(input, ExpressionHelp.CreateCheckExpression(typeof(string), checkBoxExp, GetLengMaxErrExp(maxLength) ) )); }
public IEnumerable <Expression> CreateExpression(CreatePropertyValidatorInput input) { EnumAttribute enumAttribute = input.InputProperty.GetCustomAttribute <EnumAttribute>(); if (enumAttribute == null) { yield break; } var type = input.InputProperty.PropertyType; if (!type.IsEnum) { yield break; } yield return(ExpressionHelp.CreateValidateExpression(input, ExpressionHelp.CreateCheckExpression(type, GetCheckExp(input), GetErrorExp(input) ))); }
/// <summary> /// 标记值大于指定字段名的值验证 /// </summary> /// <param name="input"></param> /// <returns></returns> private Expression GreaterThanValidator(CreatePropertyValidatorInput input) { GreaterThanAttribute greaterThanAttribute = input.InputProperty.GetCustomAttribute <GreaterThanAttribute>(); if (greaterThanAttribute == null) { return(Expression.Empty()); } Expression CheckBoxExp(Expression inputExp) { ParameterExpression valueExp = Expression.Parameter(input.InputProperty.PropertyType, "value"); MemberExpression rightExp = Expression.Property(inputExp, greaterThanAttribute.Target); BinaryExpression greaterThanExp = Expression.GreaterThan(valueExp, rightExp); UnaryExpression checkExp = Expression.IsFalse(greaterThanExp); var er = Expression.Lambda <Func <int, bool> >(checkExp, valueExp); return(er); } Expression GetErrorExp(Expression inputExp) { //$"{nameof(input.NumberOfMeals)}要大于{nameof(input.Age)},但是当前{nameof(input.NumberOfMeals)}为{input.NumberOfMeals},{nameof(input.Age)}为{input.Age}" ParameterExpression leftNameExp = Expression.Parameter(typeof(string), "leftName"); MemberExpression leftValueExp = Expression.Property(inputExp, input.InputProperty); ConstantExpression rightNameExp = Expression.Constant(greaterThanAttribute.Target, typeof(string)); MemberExpression rightValueExp = Expression.Property(inputExp, greaterThanAttribute.Target); NewArrayExpression arrExp = Expression.NewArrayInit( typeof(object), Expression.Convert(leftNameExp, typeof(object)), Expression.Convert(rightNameExp, typeof(object)), Expression.Convert(leftValueExp, typeof(object)), Expression.Convert(rightValueExp, typeof(object)) ); MethodInfo stringFormatMethod = typeof(string).GetMethod(nameof(string.Format), new Type[] { typeof(string), typeof(object[]) }); MethodCallExpression stringFormatExp = Expression.Call( stringFormatMethod, Expression.Constant("{0}要大于{1},但是当前{0}为{2},{1}为{3}", typeof(string)), arrExp); MethodCallExpression errorExp = Expression.Call(typeof(ValidateResult), nameof(ValidateResult.Error), null, stringFormatExp ); var re = Expression.Lambda <Func <string, ValidateResult> >(errorExp, leftNameExp); return(re); } return(ExpressionHelp.CreateValidateExpression(input, ExpressionHelp.CreateCheckExpression(typeof(int), CheckBoxExp, GetErrorExp ))); }
protected override IEnumerable <Expression> CreateExpressionCore(CreatePropertyValidatorInput input) { var equalToAttribute = input.InputProperty.GetCustomAttribute <EqualToAttribute>(); if (equalToAttribute == null) { yield break; } Expression CheckExpBoxFactory(Expression inputExp) { ParameterExpression valueExp = Expression.Parameter(input.InputProperty.PropertyType, "value"); MemberExpression rightExp = Expression.Property(inputExp, equalToAttribute.Target); BinaryExpression leftEqualNullExp = Expression.Equal(valueExp, Expression.Constant(null)); BinaryExpression rightEqualNullExp = Expression.Equal(rightExp, Expression.Constant(null)); UnaryExpression orToNullExp = Expression.IsTrue(Expression.OrElse(leftEqualNullExp, rightEqualNullExp)); UnaryExpression allToNullExp = Expression.IsTrue(Expression.AndAlso(leftEqualNullExp, rightEqualNullExp)); ParameterExpression resultExp = Expression.Variable(typeof(bool)); LabelTarget returnExp = Expression.Label(typeof(bool)); MethodInfo toLower = typeof(string).GetMethod(nameof(string.ToLower), Type.EmptyTypes); BinaryExpression notEqualExp = Expression.NotEqual( Expression.Call(valueExp, toLower), Expression.Call(rightExp, toLower)); ConditionalExpression ifExp = Expression.IfThenElse( allToNullExp, Expression.Assign(resultExp, Expression.Constant(false)), Expression.IfThenElse( orToNullExp, Expression.Assign(resultExp, Expression.Constant(true)), Expression.Assign(resultExp, notEqualExp) ) ); BlockExpression blockExp = Expression.Block(new[] { resultExp }, ifExp, Expression.Label(returnExp, resultExp)); var checkBoxExp = Expression.Lambda <Func <string, bool> >(blockExp, valueExp); return(checkBoxExp); } Expression ErrorExpFactory(Expression inputExp) { ParameterExpression nameExp = Expression.Parameter(typeof(string), "name"); ConstantExpression rightExp = Expression.Constant(equalToAttribute.Target, typeof(string)); MethodCallExpression stringFormatExp = Expression.Call( typeof(string), nameof(string.Format), null, Expression.Constant("{0}不等于{1}", typeof(string)), Expression.Convert(nameExp, typeof(object)), Expression.Convert(rightExp, typeof(object)) ); var errorMsg = Expression.Lambda <Func <string, ValidateResult> >(_getErrorMsgExp(stringFormatExp), nameExp); return(errorMsg); } yield return(ExpressionHelp.CreateValidateExpression(input, ExpressionHelp.CreateCheckExpression(typeof(string), CheckExpBoxFactory, ErrorExpFactory ) )); }