zoukankan      html  css  js  c++  java
  • C#特性类的使用

    特性类的使用过程:

    第一步:定义一个特性类,定义一些成员来包含验证时需要的数据;
    第二步:创建特性类实例;
    创建一个特性类的实例,里面包含着验证某一个属性或者字段需要的数据。
    将该实例关联到某个属性上面。
    第三步:使用特性类实例
    可以通过调用某个类型的GetProperties()方法,获取属性,
    然后调用类型属性成员的GetCustomAttributes()方法,获取该属性关联的特性类实例,
    然后使用查找到的特性类实例验证新建对象。

    第一步:定义特性类
    image

    第二步:创建一个特性类的实例,并关联一个属性

    public class Order
        {
            [StringLength("订单号", 6, MinLength = 3, ErrorMessage = "{0}的长度必须在{1}和{2}之间!")]//实例化一个特性类,关联到一个字段上面
            public string OrderID { get; set; }
        }

    第三步:使用特性类实例,进行验证

    class Program
        {
            #region 使用特性类的过程
    
            //验证过程
            //1.通过映射,找到成员属性关联的特性类实例,
            //2.使用特性类实例对新对象的数据进行验证
    
            //用特性类验证订单号长度
            public static bool isIDLengthValid(int IDLength, MemberInfo member)
            {
                foreach (object attribute in member.GetCustomAttributes(true)) //2.通过映射,找到成员属性上关联的特性类实例,
                {
                    if (attribute is StringLengthAttribute)//3.如果找到了限定长度的特性类对象,就用这个特性类对象验证该成员
                    {
                        StringLengthAttribute attr = (StringLengthAttribute)attribute;
                        if (IDLength < attr.MinLength || IDLength > attr.MaxLength)
                        {
                            string displayName = attr.DisplayName;
                            int maxLength = attr.MaxLength;
                            int minLength = attr.MinLength;
                            string error = attr.ErrorMessage;
                            Console.WriteLine(error, displayName,maxLength, minLength);//验证失败,提示错误
                            return false;
                        }
                        else return true;
                    }
                   
                }
                return false;
            }
    
    
            //验证订单对象是否规范
            public static bool isOrderValid(Order order)
            {
                if (order == null) return false;
                foreach (PropertyInfo p in typeof(Order).GetProperties())
                {
                    if (isIDLengthValid(order.OrderID.Length, p))//1记录下新对象需要验证的数据,
                        return true;
                }
                return false;
    
            }
            #endregion
            public static void Main()
            {
                Order order=new Order();
                do
                {
                    Console.WriteLine("请输入订单号:");
                     order.OrderID= Console.ReadLine();
                }
                while (!isOrderValid(order));
                Console.WriteLine("订单号输入正确,按任意键退出!");
                Console.ReadKey();
            }
    
        }
    总结:特性类的实例里没有验证逻辑,只有验证用到的规范数据(比如字符串长度)、提示信息等。验证逻辑需要自己写。

    image

  • 相关阅读:
    05---二叉树---20195106023---王亚威.c
    05---二叉树---20195106043---方传祥.c
    05---二叉树---20195106006---陈辉.c
    05---二叉树---20195106064---陈昕.c
    05---二叉树---20195106100---李遂勋.c
    2020下---3D建模---作业---blender
    nothing provides python(abi) = 3.8 needed by VirtualBox-6.1-6.1.16_140961_fedora32-1.x86_64
    el-table、pl-table(u-table)、ux-grid解决表格问题的实例(大数据量)
    1800*1【Codeforces Round #665 (Div. 2) D】Maximum Distributed Tree
    【Educational Codeforces Round 97 (Rated for Div. 2) C】Chef Monocarp
  • 原文地址:https://www.cnblogs.com/wllhq/p/4634410.html
Copyright © 2011-2022 走看看