Example #1
0
        /// <summary>
        /// This should be called to set the new value.
        /// </summary>
        /// <param name="value"></param>
        internal void SetValue(string value)
        {
            // Validate string Length before any conversions.
            if (ValLen != null)
            {
                if (!type.IsArray)
                {
                    if (ValLen.TrimFirst)
                    {
                        value = value.Trim();
                    }
                    if (!ValLen.IsInRange(value.Length))
                    {
                        Throw.ValidationLength(this.Name, ValLen.Min, ValLen.Max);
                    }
                }
            }

            object val = null;

            try
            {
                // Also converts arrays.
                val = Converter.ConvertFromString(value, this.type);
            }
            catch
            {
                Throw.ConversionError(this.Name, this.Type, value);
            }

            // Validate value using rules.
            ValidateValue(val);

            // Set value in the parm class, but also set value in the instance.
            this.value = val;
            if (this.instance == null)
            {
                throw new CmdException("Instance object is null.");
            }
            if (this.memberInfo == null)
            {
                throw new CmdException("MemberInfo is null.");
            }

            if (this.memberInfo is PropertyInfo)
            {
                PropertyInfo pi = (PropertyInfo)this.memberInfo;
                pi.SetValue(this.instance, val, null);
            }
            else
            {
                FieldInfo fi = (FieldInfo)this.memberInfo;
                fi.SetValue(this.instance, val);
            }

            this.beenSet = true;
        }
Example #2
0
        private void ValidateValue(object obj)
        {
            // Validate Length again.  But this time only for array elements on a string[].
            if (ValLen != null)
            {
                if (type.IsArray)
                {
                    if (type.GetElementType() == typeof(string))
                    {
                        Array sa = (string[])obj;
                        foreach (string s in sa)
                        {
                            string tmpString = s;
                            if (ValLen.TrimFirst)
                            {
                                tmpString = tmpString.Trim();
                            }
                            if (!ValLen.IsInRange(tmpString.Length))
                            {
                                Throw.ValidationLength(this.Name, ValLen.Min, ValLen.Max);
                            }
                        }
                    }
                }
            }

            Array array = null;

            if (type.IsArray)
            {
                array = (Array)obj;
            }

            // Validate Count.
            if (this.ValCount != null)
            {
                if (!ValCount.IsInRange(array.Length))
                {
                    Throw.ValidationArrayCount(this.Name, ValCount.Min, ValCount.Max);
                }
            }

            bool   inValSet    = false;
            string sObj        = null;
            bool   objIsString = false;

            if (obj is string)
            {
                sObj        = (string)obj;
                objIsString = true;
            }

            // Validate Set (i.e. select list.)
            if (this.ValSet != null)
            {
                if (type.IsArray)
                {
                    // Verify each element in the array is in the ValSet.
                    foreach (object o in array)
                    {
                        bool isInSet = false;

                        // Each o must be in the set.
                        if (o is string)
                        {
                            string os = (string)o;
                            foreach (string s in valSet)
                            {
                                if (this.valSetCaseInsensitive)
                                {
                                    if (os.Equals(s, StringComparison.OrdinalIgnoreCase))
                                    {
                                        isInSet = true;
                                        break;
                                    }
                                }
                                else
                                {
                                    if (os.Equals(s, StringComparison.Ordinal))
                                    {
                                        isInSet = true;
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            //bool isInSet = false;
                            foreach (object vo in ValSet)
                            {
                                IComparable ic = (IComparable)o;
                                if (ic.CompareTo(vo) == 0)
                                {
                                    isInSet = true;
                                    break;
                                }
                            }
                        }
                        if (!isInSet)
                        {
                            Throw.ValidationSet(this.Name, obj.ToString());
                        }
                    }
                }
                else                 // Type is not an array, so verify the value is in the ValSet.
                {
                    foreach (object o in ValSet)
                    {
                        if (objIsString)
                        {
                            if (valSetCaseInsensitive)
                            {
                                if (sObj.Equals((string)o, StringComparison.OrdinalIgnoreCase))
                                {
                                    inValSet = true;
                                    break;
                                }
                            }
                            else
                            {
                                if (sObj.Equals((string)o, StringComparison.Ordinal))
                                {
                                    inValSet = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            IComparable ic = (IComparable)obj;
                            if (ic.CompareTo(o) == 0)
                            {
                                inValSet = true;
                                break;
                            }
                        }
                    }
                    if (!inValSet)
                    {
                        Throw.ValidationSet(this.Name, obj.ToString());
                    }
                }
            }

            // Validate Range.
            if (this.valRange != null)
            {
                if (type.IsArray)
                {
                    // Validate each element in the array is within range.
                    foreach (object o in array)
                    {
                        if (!valRange.IsInRange(o))
                        {
                            Throw.ValidationRange(this.Name, valRange.Min, valRange.Max);
                        }
                    }
                }
                else
                {
                    if (!valRange.IsInRange(obj))
                    {
                        Throw.ValidationRange(this.Name, valRange.Min, valRange.Max);
                    }
                }
            }

            // Validate Pattern.
            if (this.valPattern != null)
            {
                Regex rx = new Regex(this.valPattern);
                if (type.IsArray)
                {
                    string[] sa = (string[])obj;
                    foreach (string s in sa)
                    {
                        if (!rx.IsMatch(s))
                        {
                            Throw.ValidationPattern(this.Name, valPattern);
                        }
                    }
                }
                else
                {
                    if (!rx.IsMatch((string)obj))
                    {
                        Throw.ValidationPattern(this.Name, valPattern);
                    }
                }
            }
        }