当前位置:编程学习 > C#/ASP.NET >>

ASP.NET 配置标准验证程序 ConfigurationValidatorBase

在下面的代码中,我在StrValidator类中的Validate方法中接到的value总是PersonalInfo类中Name的特性的默认值"Guatai",而不是我指定的.,求教!


 /// <summary>
    /// 个人信息
    /// </summary>
    public class PersonalInfo : ConfigurationSection
    {
        /// <summary>
        /// 姓名
        /// </summary>
        [ConfigurationProperty("Name", DefaultValue = "Guatai", IsRequired = true)]
        [StrValidator(MaxLen = 5, MinLen = 2)] 
        public string Name
        {
            get { return (string)this["Name"]; }
            set { this["Name"] = value; }
        }

        /// <summary>
        /// 年龄
        /// </summary>
        [ConfigurationProperty("Age", DefaultValue = 20, IsRequired = true)]
        // [IntegerValidator(MaxValue = 120, MinValue = 0)]
        public int Age
        {
            get { return (int)this["Age"]; }
            set { this["Age"] = value; }
        }

        /// <summary>
        /// 住址
        /// </summary>
        [ConfigurationProperty("Address", DefaultValue = "中国重庆", IsRequired = true)]
        // [StringValidator(MaxLength = 60, MinLength = 8)]
        public string Address
        {
            get { return (string)this["Address"]; }
            set { this["Address"] = value; }
        }

        /// <summary>
        /// 信用卡信息
        /// </summary>
        [ConfigurationProperty("CreditCards")]
        public Cards CreditCards
        {
            get { return (Cards)this["CreditCards"]; }
            set { this["CreditCards"] = value; }
        }
    }


// [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
    public class StrValidatorAttribute : ConfigurationValidatorAttribute
    {
        private string FRegexStr;
        /// <summary>
        /// 正则
        /// </summary>
        public string RegexStr
        {
            get { return FRegexStr; }
            set { FRegexStr = value; }
        }

        private int FMaxLen;
        /// <summary>
        /// 最大长度
        /// </summary>
        public int MaxLen
        {
            get { return FMaxLen; }
            set { FMaxLen = value; }
        }

        private int FMinLen;
        /// <summary>
        /// 最小长度
        /// </summary>
        public int MinLen
        {
            get { return FMinLen; }
            set { FMinLen = value; }
        }
          
        /// <summary>
        /// 
        /// </summary>
        public override ConfigurationValidatorBase ValidatorInstance
        {
            get
            {
                return new StrValidator(this.FRegexStr, this.FMaxLen, this.FMinLen);
            }
        }
    }




public class StrValidator : ConfigurationValidatorBase
    {
        string FRegExStr;
        int FMaxLen;
        int FMinLen;

        public StrValidator(string regexstr, int maxlen, int minlen)
        {
            this.FRegExStr = regexstr;
            this.FMaxLen = maxlen;
            this.FMinLen = minlen;
        }

        public override bool CanValidate(Type type)
        {
            return type == typeof(string);
        }

        public override void Validate(object value)
        {
            try
            {
                string val = (string)value;
                if (!string.IsNullOrEmpty(this.FRegExStr))
                {
                    Regex ex = new Regex(this.FRegExStr);
                    if (!ex.IsMatch(val))
                        throw new Exception("与正则表达式指定的格式不匹配");
                }
                if (this.FMaxLen>0)
                {
                    if (val.Length > this.FMaxLen)
                    {
                        throw new Exception("长度不能大于" + this.FMaxLen.ToString());
                    }
                }
                if (this.FMinLen >0)
                {
                    if (val.Length < this.FMinLen)
                    {
                        throw new Exception("长度不能小于" + this.FMinLen.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

--------------------编程问答-------------------- 在PersonalInfo类中,用一个自定义类StrValidator来验证属性Name是否合法!

 
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,