zoukankan      html  css  js  c++  java
  • C#:实体类中做数据验证

    主要是在实体类中验证

    using System;

    namespace Jone.Function.attribute
    {
            /// <summary>
            /// 附加在数据实体用于描述如何验证合法性
            /// </summary>
            [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
            public sealed class ValidateAttribute : Attribute
            {
                private bool _allowNull = true;
                private string _regEx;
                private string _description;
                private string _ValidateType;
                private string _instruction;

                #region 方法
                public ValidateAttribute()
                {
                }
                public ValidateAttribute(bool AllowNull)
                {
                    _allowNull = AllowNull;
                }
                public ValidateAttribute(bool AllowNull, string ValidateType)
                {
                    _allowNull = AllowNull;
                    _ValidateType = ValidateType;
                }
                #endregion

                #region 属性
                /// <summary>
                /// 描述
                /// </summary>
                public string Description
                {
                    get { return _description; }
                    set { _description = value; }
                }
                /// <summary>
                /// 验证类型
                /// </summary>
                public string ValidateType
                {
                    get
                    {
                        return _ValidateType;
                    }
                    set
                    {
                        _ValidateType = value;
                    }
                }

                /// <summary>
                /// 是否可以空
                /// </summary>
                public bool AllowNull
                {
                    get
                    {
                        return _allowNull;
                    }
                    set
                    {
                        _allowNull = value;
                    }
                }
                /// <summary>
                /// 用于验证的正则表达式
                /// </summary>
                public string RegEx
                {
                    get
                    {
                        return _regEx;
                    }
                    set
                    {
                        _regEx = value;
                    }
                }

                /// <summary>
                /// 对于正确格式的描述
                /// </summary>
                public string Instruction
                {
                    get
                    {
                        return _instruction;
                    }
                    set
                    {
                        _instruction = value;
                    }
                }
                #endregion
            }

            #region 使用示例
            //public class 使用示例
            //{
            //    public 使用示例()
            //    {

            //    }
            //    private int _id;
            //    private string _name;
            //    //[Validate(RegEx = "^\d{17}(http://www.cnblogs.com/luomingui/admin/file:///d|x)$", Description = "身份证号码")]
            //    [Validate(RegEx = "", Description = "")]
            //    public int id
            //    {
            //        set { _id = value; }
            //        get { return _id; }
            //    }
            //    [Validate(AllowNull = false, Description = "姓名")]
            //    public string name
            //    {
            //        set { _name = value; }
            //        get { return _name; }
            //    }
            //}
            #endregion
        }

  • 相关阅读:
    千万别用树套树 【题意:有多少线段完全覆盖某一线段【树状数组维护】】【模板题】
    Codeforces Round #590 (Div. 3)【D题:26棵树状数组维护字符出现次数】
    Codeforces Round #590 (Div. 3)【D题:维护26棵树状数组【好题】】
    Codeforces Round #350 (Div. 2) A B C D1 D2 水题【D2 【二分+枚举】好题】
    AtCoder Beginner Contest 142【D题】【判断素数的模板+求一个数的因子的模板】
    AtCoder Beginner Contest 116 D
    序列自动机【模板】
    题解 CF1428G Lucky Numbers (Easy Version and Hard Version)
    题解 CF1428F Fruit Sequences
    题解 P5401 [CTS2019]珍珠
  • 原文地址:https://www.cnblogs.com/Alex80/p/4372968.html
Copyright © 2011-2022 走看看