下面以定義一個簡單數據庫表的映射實體類來說明相關的使用方法,基 " /> 久久精品国产亚洲AV蜜臀 ,国产精品亚欧美一区二区三区,国产精品免费一区二区三区视频

天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

.net使用自定義類屬性實例

一般來說,在.NET中可以使用Type.GetCustomAttributes獲取類上的自定義屬性,可以使用PropertyInfo.GetCustomAttributes獲取屬性信息上的自定義屬性。
 
下面以定義一個簡單數據庫表的映射實體類來說明相關的使用方法,基于自定義類屬性和自定義類中的屬性的自定義屬性,可以方便的進行類標記和類中屬性的標記
 
創(chuàng)建一個類的自定義屬性,用于標識數據庫中的表名稱,需要繼承自Attribute類:

復制代碼 代碼如下:[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class TableAttribute : Attribute
{
        private readonly string _TableName = "";
        public TableAttribute(string tableName)
        {
            this._TableName = tableName;
        }
        public string TableName
        {
            get { return this._TableName; }
        }
}

創(chuàng)建一個屬性的自定義屬性,用于標識數據庫表中字段的名稱,需要繼承自Attribute類:

復制代碼 代碼如下:[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public class FieldAttribute : Attribute
{
        private readonly string _FieldName = "";    ///數據庫的字段名稱
        private System.Data.DbType _Type = System.Data.DbType.String;   ///數據庫的字段類型
 
        public FieldAttribute(string fieldName)
       {
              this._FieldName=fieldName;
       }
 
        public FieldAttribute(string fieldName,System.Data.DbType type)
       {
              this._FieldName=fieldName;
              this._Type=type;
       }
 
       public string FieldName
        {
            get { return this._FieldName; }
        }
 
        public System.Data.DbType Type
        {
             get{return this._Type;}
        }
}
 
創(chuàng)建一個數據實體基類:

復制代碼 代碼如下:public class BaseEntity
{
        public BaseEntity()
        {
        }
 
         /// <summary>
        /// 獲取表名稱
        /// </summary>
        /// <returns></returns>
        public string GetTableName()
        {
            Type type = this.GetType();
            object[] objs = type.GetCustomAttributes(typeof(TableAttribute), true);
            if (objs.Length <= 0)
            {
                throw new Exception("實體類沒有標識TableAttribute屬性");
            }
            else
            {
                object obj = objs[0];
                TableAttribute ta = (TableAttribute)obj;
                return ta.TableName;                            //獲取表名稱
            }
        }
        /// <summary>
        /// 獲取數據實體類上的FieldAttribute
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public FieldAttribute GetFieldAttribute(string propertyName)
        {
            PropertyInfo field = this.GetType().GetProperty(propertyName);
            if (field == null)
            {
                throw new Exception("屬性名" + propertyName + "不存在");
            }
            object[] objs = field.GetCustomAttributes(typeof(FieldAttribute), true);
            if (objs.Length <= 0)
            {
                throw new Exception("類體屬性名" + propertyName + "沒有標識FieldAttribute屬性");
            }
            else
            {
                object obj = objs[0];
                FieldAttribute fieldAttribute=(FieldAttribute)obj;
                fieldAttribute.FieldValue=field.GetValue(this,null);
                return fieldAttribute;
            }
        }
}
 
創(chuàng)建數據實體:

復制代碼 代碼如下:[Table("Wincms_Dictionary")]            ///映射到數據庫的Wincms_Dictionary表
public class Wincms_Dictionary : BaseEntity
{
         private int _DictionaryId;
 
         public Wincms_Dictionary()
         {
         }
 
        [Field("DictionaryId",DbType.Int32)]                ///映射到數據庫的Wincms_Dictionary表中的字段
        public int DictionaryId
        {
            get { return this._DictionaryId; }
            set
            {
                this._DictionaryId = value;
            }
        }
}
 
///基于實體類獲取實體對應的表名稱和字段名稱
public class Test
{
 
       public static void main(string[] args)
        {
               Wincms_Dictionary dict=new Wincms_Dictionary();
               Console.WriteLine("表名稱:"+GetTableName(dict));
               Console.WriteLine("字段名稱:"+GetFieldName(dict,"DictionaryId"));
               Console.Read();
        }
 
       ///獲取實體表名稱
       public  static string GetTableName(BaseEntity entity)
       {
                return entity.GetTableName();
       }
 
       ///獲取實體字段名稱
       public static string GetFieldName(BaseEntity entity,string propertyName)
       {
              FieldAttribute fieldAttribute=entity.GetFieldAttribute(propertyName);
              return fieldAttribute.FieldName;
       }
}
輸出結果為:

復制代碼 代碼如下:表名稱:Wincms_Dictionary
字段名稱:DictionaryId

希望本文所述對大家的.NET程序設計有所幫助。

AspNet技術.net使用自定義類屬性實例,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 嘟嘟嘟在线视频免费观看高清中文 | 特级毛片AAAAAA | 欧美一区二区视频97色伦 | 俄罗斯12x13x处 | 在线免费中文字幕 | 97国产精品视频在线观看 | 久久9精品区-无套内射无码 | 国产欧美在线亚洲一区刘亦菲 | 伊人久久免费 | 久久久久久久99精品免费观看 | 亚洲AV成人片色在线观看网站 | 帅哥操美女 | 久久青草免费91线频观看站街 | 色迷迷导航 | 玖玖热视频一区二区人妻 | 免费精品国偷自产在线在线 | 国产精亚洲视频综合区 | 无人区在线日本高清免费 | AV天堂AV亚洲啪啪久久无码 | 永久免费精品精品永久-夜色 | 姉调无修版ova国语版 | 国产精品色午夜视频免费看 | 18岁男人女人插孔 | 最近中文字幕MV高清在线 | 亚洲国产av | 99视频偷窥在线精品国自产拍 | 色综合色综合久久综合频道 | 国产日韩欧美高清免费视频 | 亚洲成人在线免费 | 国产跪地吃黄金喝圣水合集 | 3D内射动漫同人资源在线观看 | 久久久精品久久久久三级 | 久久精品99国产精品日本 | 中文字幕一区二区三区在线不卡 | 国产无遮挡又黄又爽在线视频 | 最近最新的日本字幕MV | 久久99国产精品一区二区 | 国语精彩对白2021 | 蜜臀久久99精品久久久久久做爰 | 久久国产亚洲电影天堂 | 四虎国产精品永久一区高清 |