Exemple #1
0
 public RuleMethod(object target, RuleHandler handler, RuleArgs args)
 {
     _target   = target;
     _handler  = handler;
     _args     = args;
     _ruleName = _handler.Method.Name + "!" + _args.ToString();
 }
Exemple #2
0
 public RuleMethod(object target, RuleHandler handler, string propertyName)
 {
     _target   = target;
     _handler  = handler;
     _args     = new RuleArgs(propertyName);
     _ruleName = _handler.Method.Name + "!" + _args.ToString();
 }
Exemple #3
0
        public void AddRule(RuleHandler handler, RuleArgs args)
        {
            // get the list of rules for the property
            List <RuleMethod> list = GetRulesForProperty(args.PropertyName);

            // we have the list, add our new rule
            list.Add(new RuleMethod(_target, handler, args));
        }
Exemple #4
0
        public static bool StringRequired(object target, RuleArgs e)
        {
            string value = (string)Utilities.CallByName(
                target, e.PropertyName, CallType.Get);

            if (string.IsNullOrEmpty(value))
            {
                e.Description = string.Format(Resources.StringRequiredRule, e.PropertyName);
                return(false);
            }
            return(true);
        }
Exemple #5
0
        public static bool IntegerMaxValue(object target, RuleArgs e)
        {
            int max   = ((IntegerMaxValueRuleArgs)e).MaxValue;
            int value = (int)Utilities.CallByName(target, e.PropertyName, CallType.Get);

            if (value > max)
            {
                e.Description = String.Format(Resources.MaxValueRule,
                                              e.PropertyName, max.ToString());
                return(false);
            }
            return(true);
        }
Exemple #6
0
        public static bool RegExMatch(object target, RuleArgs e)
        {
            Regex rx = ((RegExRuleArgs)e).RegEx;

            if (!rx.IsMatch(Utilities.CallByName(target, e.PropertyName, CallType.Get).ToString()))
            {
                e.Description = String.Format(Resources.RegExMatchRule, e.PropertyName);
                return(false);
            }
            else
            {
                return(true);
            }
        }
Exemple #7
0
        public static bool StringMaxLength(
            object target, RuleArgs e)
        {
            int    max   = ((MaxLengthRuleArgs)e).MaxLength;
            string value = (string)Utilities.CallByName(
                target, e.PropertyName, CallType.Get);

            if (!String.IsNullOrEmpty(value) && (value.Length > max))
            {
                e.Description = String.Format(
                    Resources.StringMaxLengthRule,
                    e.PropertyName, max.ToString());
                return(false);
            }
            return(true);
        }
Exemple #8
0
        public static bool MinValue <T>(object target, RuleArgs e)
        {
            T min   = (T)((MinValueRuleArgs <T>)e).MinValue;
            T value = (T)Utilities.CallByName(
                target, e.PropertyName, CallType.Get);
            bool result;
            Type pType = typeof(T);

            if (pType.IsPrimitive)
            {
                if (pType.Equals(typeof(int)))
                {
                    int v1 = Convert.ToInt32(value);
                    int v2 = Convert.ToInt32(min);
                    result = (v1 >= v2);
                }
                else if (pType.Equals(typeof(bool)))
                {
                    bool v1 = Convert.ToBoolean(value);
                    bool v2 = Convert.ToBoolean(min);
                    result = (v1 = v2);
                }
                else if (pType.Equals(typeof(float)))
                {
                    float v1 = Convert.ToSingle(value);
                    float v2 = Convert.ToSingle(min);
                    result = (v1 >= v2);
                }
                else if (pType.Equals(typeof(double)))
                {
                    double v1 = Convert.ToDouble(value);
                    double v2 = Convert.ToDouble(min);
                    result = (v1 >= v2);
                }
                else if (pType.Equals(typeof(byte)))
                {
                    byte v1 = Convert.ToByte(value);
                    byte v2 = Convert.ToByte(min);
                    result = (v1 >= v2);
                }
                else if (pType.Equals(typeof(char)))
                {
                    char v1 = Convert.ToChar(value);
                    char v2 = Convert.ToChar(min);
                    result = (v1 >= v2);
                }
                else if (pType.Equals(typeof(short)))
                {
                    short v1 = Convert.ToInt16(value);
                    short v2 = Convert.ToInt16(min);
                    result = (v1 >= v2);
                }
                else if (pType.Equals(typeof(long)))
                {
                    long v1 = Convert.ToInt64(value);
                    long v2 = Convert.ToInt64(min);
                    result = (v1 >= v2);
                }
                else if (pType.Equals(typeof(ushort)))
                {
                    ushort v1 = Convert.ToUInt16(value);
                    ushort v2 = Convert.ToUInt16(min);
                    result = (v1 >= v2);
                }
                else if (pType.Equals(typeof(uint)))
                {
                    uint v1 = Convert.ToUInt32(value);
                    uint v2 = Convert.ToUInt32(min);
                    result = (v1 >= v2);
                }
                else if (pType.Equals(typeof(ulong)))
                {
                    ulong v1 = Convert.ToUInt64(value);
                    ulong v2 = Convert.ToUInt64(min);
                    result = (v1 >= v2);
                }
                else if (pType.Equals(typeof(sbyte)))
                {
                    sbyte v1 = Convert.ToSByte(value);
                    sbyte v2 = Convert.ToSByte(min);
                    result = (v1 >= v2);
                }
                else
                {
                    throw new ArgumentException(Resources.PrimitiveTypeRequired);
                }
            }
            else  // not primitive
            {
                throw new ArgumentException(Resources.PrimitiveTypeRequired);
            }

            if (!result)
            {
                e.Description = string.Format(Resources.MinValueRule,
                                              e.PropertyName, min.ToString());
                return(false);
            }
            else
            {
                return(true);
            }
        }