Пример #1
0
        public override Expression <Func <PropertyValidationContext <OBJECT_TYPE, PROPERTY_TYPE>, ValidationResult> > GetValidateExpression <OBJECT_TYPE, PROPERTY_TYPE>()
        {
            if (typeof(PROPERTY_TYPE) != typeof(string))
            {
                throw new InvalidOperationException($"The type {typeof(PROPERTY_TYPE).Name} must be a string type");
            }

            var propertyValidationContext = Expression.Parameter(typeof(PropertyValidationContext <OBJECT_TYPE, PROPERTY_TYPE>), "propertyValidationContext");

            var minimumCheck = Expression.Call
                               (
                null,
                ValidationResult.CreateResultMethodInfo,
                Expression.GreaterThanOrEqual(Expression.Property(PropertyValidationContext <OBJECT_TYPE, PROPERTY_TYPE> .GetCurrentValueExpression(propertyValidationContext), "Length"), Expression.Constant(MinimumLength)),
                Expression.Convert(propertyValidationContext, typeof(IPropertyValidationContext))
                               );

            var maximumCheck = Expression.Call
                               (
                null,
                ValidationResult.CreateResultMethodInfo,
                Expression.LessThanOrEqual(Expression.Property(PropertyValidationContext <OBJECT_TYPE, PROPERTY_TYPE> .GetCurrentValueExpression(propertyValidationContext), "Length"), Expression.Constant(MaximumLength)),
                Expression.Convert(propertyValidationContext, typeof(IPropertyValidationContext))
                               );

            var ifNullResult = Expression.Call
                               (
                null,
                ValidationResult.CreateResultMethodInfo,
                Expression.Constant(false),
                Expression.Convert(propertyValidationContext, typeof(IPropertyValidationContext))
                               );

            var body = Expression.Condition
                       (
                Expression.Equal(PropertyValidationContext <OBJECT_TYPE, PROPERTY_TYPE> .GetCurrentValueExpression(propertyValidationContext), Expression.Constant(null)),
                ifNullResult,
                Expression.Call(minimumCheck, ValidationResult.MergeWithMethodInfo, maximumCheck)
                       );

            return(Expression.Lambda <Func <PropertyValidationContext <OBJECT_TYPE, PROPERTY_TYPE>, ValidationResult> >(body, propertyValidationContext));
        }
        public override Expression <Func <PropertyValidationContext <OBJECT_TYPE, PROPERTY_TYPE>, ValidationResult> > GetValidateExpression <OBJECT_TYPE, PROPERTY_TYPE>()
        {
            var type = typeof(PROPERTY_TYPE);

            if (Nullable.GetUnderlyingType(type) != null)
            {
                type = Nullable.GetUnderlyingType(type);
            }

            switch (Type.GetTypeCode(type))
            {
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
                break;

            default:
                throw new NotSupportedException("ValueConstraint: " + typeof(PROPERTY_TYPE));
            }

            PROPERTY_TYPE minimumValue, maximumValue;

            if (this.MinimumValue != null)
            {
                minimumValue = (PROPERTY_TYPE)ChangeType(this.MinimumValue, typeof(PROPERTY_TYPE));
            }
            else
            {
                minimumValue = (PROPERTY_TYPE)typeof(PROPERTY_TYPE).GetDefaultValue();
            }

            if (this.MaximumValue != null)
            {
                maximumValue = (PROPERTY_TYPE)ChangeType(this.MaximumValue, typeof(PROPERTY_TYPE));
            }
            else
            {
                var underlyingType = Nullable.GetUnderlyingType(typeof(PROPERTY_TYPE)) ?? typeof(PROPERTY_TYPE);

                var field = underlyingType.GetField("MaxValue", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);

                if (field != null)
                {
                    maximumValue = (PROPERTY_TYPE)field.GetValue(null);
                }
                else
                {
                    maximumValue = (PROPERTY_TYPE)typeof(PROPERTY_TYPE).GetDefaultValue();
                }
            }

            var propertyValidationContext = Expression.Parameter(typeof(PropertyValidationContext <OBJECT_TYPE, PROPERTY_TYPE>), "propertyValidationContext");

            Expression body = null;

            var minimumCheck = Expression.Call
                               (
                null,
                ValidationResult.CreateResultMethodInfo,
                Expression.GreaterThanOrEqual(PropertyValidationContext <OBJECT_TYPE, PROPERTY_TYPE> .GetCurrentValueExpression(propertyValidationContext), Expression.Constant(minimumValue, typeof(PROPERTY_TYPE))),
                Expression.Convert(propertyValidationContext, typeof(IPropertyValidationContext))
                               );

            var maximumCheck = Expression.Call
                               (
                null,
                ValidationResult.CreateResultMethodInfo,
                Expression.LessThanOrEqual(PropertyValidationContext <OBJECT_TYPE, PROPERTY_TYPE> .GetCurrentValueExpression(propertyValidationContext), Expression.Constant(maximumValue, typeof(PROPERTY_TYPE))),
                Expression.Convert(propertyValidationContext, typeof(IPropertyValidationContext))
                               );

            body = Expression.Call(minimumCheck, ValidationResult.MergeWithMethodInfo, maximumCheck);

            if (Nullable.GetUnderlyingType(typeof(PROPERTY_TYPE)) != null)
            {
                var nullCheck = Expression.Equal(PropertyValidationContext <OBJECT_TYPE, PROPERTY_TYPE> .GetCurrentValueExpression(propertyValidationContext), Expression.Constant(null));

                body = Expression.Condition(nullCheck, Expression.Field(null, typeof(ValidationResult).GetField("Success", BindingFlags.Static | BindingFlags.Public)), body);
            }

            return(Expression.Lambda <Func <PropertyValidationContext <OBJECT_TYPE, PROPERTY_TYPE>, ValidationResult> >(body, propertyValidationContext));
        }
Пример #3
0
 public abstract ValidationResult Validate(PropertyValidationContext <OBJECT_TYPE, PROPERTY_TYPE> propertyValidationContext);
Пример #4
0
 public virtual ValidationResult Validate <OBJECT_TYPE, PROPERTY_TYPE>(PropertyValidationContext <OBJECT_TYPE, PROPERTY_TYPE> propertyValidationContext)
 {
     throw new NotImplementedException();
 }
Пример #5
0
 public override ValidationResult Validate(PropertyValidationContext <OBJECT_TYPE, PROPERTY_TYPE> propertyValidationContext)
 {
     return(this.attribute.Validate(propertyValidationContext));
 }