C#特性Attribute用个简单的Dome理解

C#特性Attribute官网

接下来通过自定义邮箱特性,简单看看特性用法,特性用途很多,如用作过滤器的筛选,权限验证等,接下来直接看代码。

首先定义一个学生类

using MyAttribute.NewAttributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyAttribute
{
    public class student
    {
        [Key]
        public int Id { get; set; }

        [StringLength(50,6)]
        public string Name { get; set; }

        [EmailAddress]
        public string Email { get; set; }

        [Required]
        public int Age { get; set; }

        [Display(Name ="电话号码")]
        public string PhoneNumber { get; set; }
    }
}

再定义一个抽象的通用的特性类,这样方便后面代码的通用性。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyAttribute
{
    public abstract class CommonValidateAttribute:Attribute
    {
        public abstract bool Isvalidate(object validate);
    }
}

里面就定义了一个抽象方法,用于查看当前属性或者其他类型有没有该特性

以上面学生邮箱特性为例,定义一个邮箱特性,继承我们刚刚的抽象类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MyAttribute.NewAttributes
{
    public class EmailAddressAttribute : CommonValidateAttribute
    {
        public override bool Isvalidate(object validate)
        {
            Regex regex = new Regex(@"[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?");
            if (regex.IsMatch(validate.ToString()))
                return true;
            else
                return false;
        }
    }
}

定义抽象类的好处就是,你再自定义其他特性的时候如StringLength,可以直接继承,方便后面获取特性值得时候,一个方法就可以解决。

然后定义验证特性类。用于查看模型是否有效

using MyAttribute.NewAttributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyAttribute
{
  public   class CustomeAttribute
    {
        public static bool Validate<T>(T mode) where T : class 
        {
            //获取所有的属性
            var propertyInfo = mode.GetType().GetProperties();
            //遍历和读取属性
            foreach (var prop in propertyInfo)
            {
                //检查属性上面有没有用到这个特性
                if (prop.IsDefined(typeof(CommonValidateAttribute), true)) 
                {
                    //获取属性上的所有继承CommonValidateAttribute的特性
                    var attributes = prop.GetCustomAttributes(typeof(CommonValidateAttribute), true);
                    foreach (var item in attributes)
                    {
                        CommonValidateAttribute attribute = item as CommonValidateAttribute;
                        if (!attribute.Isvalidate(prop.GetValue(mode))) 
                        {
                            return false;
                        }
                    }
                
                }
            }
            return true;
        }
    }
}

最后调用验证

using System;

namespace MyAttribute
{
    class Program
    {
        static void Main(string[] args)
        {
            student s = new student();
            s.Email = "13010016@qq.com";
            student s1 = new student();
            s1.Email = "130";

           var flage =  CustomeAttribute.Validate<student>(s);
           var flage1 = CustomeAttribute.Validate<student>(s1);

            Console.WriteLine(flage);
            Console.WriteLine(flage1);
            Console.Read();
        }
    }
}

结果
True
False

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值