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
    }

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/luomingui/archive/2008/09/14/2874588.aspx

作者:罗敏贵
邮箱:minguiluo@163.com
QQ群:34178394 建群 主要是寻找志同道合的人士一起学习和讨论自己的所学所思
出处:http://luomingui.cnblogs.com/
说明:专注于微软平台项目架构、熟悉设计模式、架构设计、敏捷个人和项目管理。现主要从事WinForm、ASP.NET、等方面的项目开发、架构、管理工作。文章为作者平时里的思考和练习,可能有不当之处,请博客园的园友们多提宝贵意见。
知识共享许可协议本作品采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可。

原文地址:https://www.cnblogs.com/luomingui/p/1674213.html