C#:實體類中做資料驗證

來源:互聯網
上載者:User

主要是在實體類中驗證

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>
            /// 用於驗證的Regex
            /// </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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.