• 标签AttributeUsage 使用


    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]

    三个参数  第一个是标签作用范围可以是类可以是属性AttributeTargets.Property。。。第二个参数用此标签的类可以用多个标签,第三个参数用此标签的类可不可以继承。

    下面是对于属性的标签的使用

     [AttributeUsage(AttributeTargets.Property)]
        public class StringLengthAttribute: Attribute
        {
            private int _maximumLength;
            public StringLengthAttribute(int maximumLength)
            {
                _maximumLength = maximumLength;
            }
    
            public int MaximumLength
            {
                get { return _maximumLength; }
            }
        }
    
        public class People
        {
            [StringLength(8)]
            public string Name { get; set; }
    
            [StringLength(15)]
            public string Description { get; set; }
        }
    
        public class ValidationModel
        {
    
            public void Validate(object obj)
            {
                var t = obj.GetType();
    
                //由于我们只在Property设置了Attibute,所以先获取Property
                var properties = t.GetProperties();
                foreach (var property in properties)
                {
    
                    //这里只做一个stringlength的验证,这里如果要做很多验证,需要好好设计一下,千万不要用if elseif去链接
                    //会非常难于维护,类似这样的开源项目很多,有兴趣可以去看源码。
                    if (!property.IsDefined(typeof(StringLengthAttribute), false)) continue;
    
                    var attributes = property.GetCustomAttributes(true);
                    foreach (var attribute in attributes)
                    {
                        //这里的MaximumLength 最好用常量去做
                        var maxinumLength = (int)attribute.GetType().
                          GetProperty("MaximumLength").
                          GetValue(attribute);
    
                        //获取属性的值
                        var propertyValue = property.GetValue(obj) as string;
                        if (propertyValue == null)
                            throw new Exception("exception info");//这里可以自定义,也可以用具体系统异常类
    
                        if (propertyValue.Length > maxinumLength)
                            throw new Exception(string.Format("属性{0}的值{1}的长度超过了{2}", property.Name, propertyValue, maxinumLength));
                    }
                }
    
            }
        }
            private void button1_Click(object sender, EventArgs e)
            {
    
                var people = new People()
                {
                    Name = "qwadzc",
                    Description = "desco;o;opo;po;po;oription"
                };
                try
                {
                    new ValidationModel().Validate(people);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.ReadLine();
    
            }
  • 相关阅读:
    chapter02“良/恶性乳腺癌肿瘤预测”的问题
    ASCII编码和Unicode编码的区别
    Spring AOP概述
    Spring 基于注解的配置
    Spring Bean作用域&FactoryBean
    Spring <bean> 之间的关系&整合多个配置文件
    Spring 方法注入
    Spring 简化装配Bean的配置方式
    Spring 注入参数详解
    vue-router 导航守卫
  • 原文地址:https://www.cnblogs.com/dzh1990/p/10154690.html
Copyright © 2020-2023  润新知