// Customer Attribute
namespace UserAttribute
{
    [AttributeUsage(AttributeTargets.Property,AllowMultiple=false,Inherited=false)]
    public class MaxLength:Attribute
    {
        private int max;
        private int min;        public MaxLength(int max, int min)
        {
            this.max = max;
            this.min = min;
        }
        public int Max
        {
            set { this.max = value; }
            get { return this.max; }
        }        public int Min
        {
            set { this.min = value; }
            get { return this.min; }
        }        public string Message
        {
            set;
            get;
        }
        public bool Valid(string value)
        {
            if (value.Length >= min && value.Length <= max)
                return true;
            return false;
        }
    }
}// class property 
namespace UserAttribute
{
    public class User
    {
        [MaxLength(10, 5, Message = "The lenth between 10 and 5.")]
        public string FirstName
        {
            set;
            get;
        }
    }
}
另外还有一个类是验证Property是否有MaxLength的,我想知道程序运行的时候是怎么验证的!
谢谢!

解决方案 »

  1.   

    主要是应用了反射的机制:class Program
        {
            static void Main(string[] args)
            {
                User u = new User();
                u.Name = "aa";            //
                Type t = u.GetType();
                
                            MemberInfo[] mi = t.GetMembers();
                foreach (MemberInfo m in mi)
                {
                    object[] ats = m.GetCustomAttributes(false);
                    foreach (Attribute atr in ats)
                    {
                        if (atr is MaxLengthAttribute)
                        {
                            MaxLengthAttribute matr = (MaxLengthAttribute)atr;
                            matr.Valid(u.Name);
                        }
                    }
                }            cp.WriteLine(u.Name);
                cp.ReadLine();
            }
            
        }    [AttributeUsage(AttributeTargets.All,AllowMultiple=true,Inherited=false)]
        public class MaxLengthAttribute : Attribute
        {
            public int Max {get;set;}
            public int Min {get;set;}
            public string Message {get;set;}        public MaxLengthAttribute(int pmax,int pmin,string msg)
            {
                cp.WriteLine("attr");
                this.Max = pmax;
                this.Min = pmin;
                this.Message = msg;
            }        public void Valid(string v)
            {
                if (v.Length >= Min && v.Length <= Max)
                    cp.WriteLine("success");
                else
                    cp.WriteLine(this.Message);
            }
        }    public class User
        {
            [MaxLength(10,3,"length is not valid")]
            public string Name { get; set; }
        }
      

  2.   

    也就是说每次都要对业务对象进行验证吗,为什么我们用.NET内置的Attribute属性的时候不用验证呢?
      

  3.   

    其实我就想知道这个应该在什么地方处理?
    比如在Model中验证或者在Business中验证?