特性

简介

1. 定义一个特性类,定义一些成员来包含验证时需要的数据

2. 创建特性类实例,里面包含着验证某一个属性或者字段需要的数据,将该实例关联到某个属性上面

3. 使用特性类实例

可以通过调用某个类型的GetProperties()方法,获取属性,
然后调用类型属性成员的GetCustomAttributes()方法,获取该属性关联的特性类实例

    class Program
    {
     
        static void Main(string[] args)
        {
            Student student = new Student();
            student.StuName = "中国飒飒啊奥奥所撒撒撒撒奥所撒撒撒撒";
            InvokeManage.ManageStudent<Student>(student);
            Console.WriteLine();
        }
    }

    public class InvokeManage
    {
        public static void ManageStudent<T>(T student) where T:Student
        {
            if(student == null)
            {
                return;
            }
            Type type = student.GetType();
            // 判断类上是否定义了特性
            if(type.IsDefined(typeof(StringLengthAttribute),true)) {
       
            }
            else
            {
                // 获取属性值
                foreach(PropertyInfo prop in type.GetProperties())
                {
                    // 判断属性中是否有自定义属性
                    if(prop.IsDefined(typeof(StringLengthAttribute),true))
                    {
                        foreach(object attribute in prop.GetCustomAttributes(typeof(StringLengthAttribute),true))
                        {
                            StringLengthAttribute attr = (StringLengthAttribute)attribute;
                            int length = student.StuName.Length;
                            int minLength = attr.MinLength;
                            int maxLength = attr.MaxLength;
                            if (length < minLength || length > maxLength)
                            {
                                string displayName = attr.DisplayName;
                                string error = attr.ErrorMessage;
                                Console.WriteLine(error, displayName, minLength, maxLength);//验证失败,提示错误
                            }
                            
                        }
                    }
                }
                // 获取方法值
                foreach(MethodInfo methodInfo in type.GetMethods())
                {
                    if(methodInfo.IsDefined(typeof(StringLengthAttribute),true))
                    {
                        object[] oAttributeArrayMethod = methodInfo.GetCustomAttributes(typeof(StringLengthAttribute), true);
                        foreach (StringLengthAttribute attribute in oAttributeArrayMethod)
                        {
                            
                        }
                    }
                }
            }
        }
    }


    public class Student
    {
        [StringLength("学生姓名",10,MinLength =2,ErrorMessage ="{0}的长度在{1}和{2}之间")]
        public string StuName { get; set; }
    }

    // 用在类和属性上面
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
    public class StringLengthAttribute:Attribute
    {
        public string DisplayName { get; private set; }
        public int MaxLength { get; private set; }
        public string ErrorMessage { get; set; } // 定义失败提示
        public int MinLength { get; set; }
        /// <summary>
        /// 定义构造函数
        /// </summary>
        /// <param name="displayName"></param>
        /// <param name="maxLength"></param>
        public StringLengthAttribute(string displayName,int maxLength)
        {
            this.DisplayName = displayName;
            this.MaxLength = maxLength;
        }
    }

 

posted @ 2020-03-16 14:52  bradleydan  阅读(114)  评论(0)    收藏  举报