下面以定義一個(gè)簡(jiǎn)單數(shù)據(jù)庫(kù)表的映射實(shí)體類(lèi)來(lái)說(shuō)明相關(guān)的使用方法,基 " /> 欧美日韩片,精品999,国产japanese孕妇孕交

一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

.net使用自定義類(lèi)屬性實(shí)例

一般來(lái)說(shuō),在.NET中可以使用Type.GetCustomAttributes獲取類(lèi)上的自定義屬性,可以使用PropertyInfo.GetCustomAttributes獲取屬性信息上的自定義屬性。
 
下面以定義一個(gè)簡(jiǎn)單數(shù)據(jù)庫(kù)表的映射實(shí)體類(lèi)來(lái)說(shuō)明相關(guān)的使用方法,基于自定義類(lèi)屬性和自定義類(lèi)中的屬性的自定義屬性,可以方便的進(jìn)行類(lèi)標(biāo)記和類(lèi)中屬性的標(biāo)記
 
創(chuàng)建一個(gè)類(lèi)的自定義屬性,用于標(biāo)識(shí)數(shù)據(jù)庫(kù)中的表名稱(chēng),需要繼承自Attribute類(lèi):

復(fù)制代碼 代碼如下:[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)建一個(gè)屬性的自定義屬性,用于標(biāo)識(shí)數(shù)據(jù)庫(kù)表中字段的名稱(chēng),需要繼承自Attribute類(lèi):

復(fù)制代碼 代碼如下:[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public class FieldAttribute : Attribute
{
        private readonly string _FieldName = "";    ///數(shù)據(jù)庫(kù)的字段名稱(chēng)
        private System.Data.DbType _Type = System.Data.DbType.String;   ///數(shù)據(jù)庫(kù)的字段類(lèi)型
 
        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)建一個(gè)數(shù)據(jù)實(shí)體基類(lèi):

復(fù)制代碼 代碼如下:public class BaseEntity
{
        public BaseEntity()
        {
        }
 
         /// <summary>
        /// 獲取表名稱(chēng)
        /// </summary>
        /// <returns></returns>
        public string GetTableName()
        {
            Type type = this.GetType();
            object[] objs = type.GetCustomAttributes(typeof(TableAttribute), true);
            if (objs.Length <= 0)
            {
                throw new Exception("實(shí)體類(lèi)沒(méi)有標(biāo)識(shí)TableAttribute屬性");
            }
            else
            {
                object obj = objs[0];
                TableAttribute ta = (TableAttribute)obj;
                return ta.TableName;                            //獲取表名稱(chēng)
            }
        }
        /// <summary>
        /// 獲取數(shù)據(jù)實(shí)體類(lèi)上的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("類(lèi)體屬性名" + propertyName + "沒(méi)有標(biāo)識(shí)FieldAttribute屬性");
            }
            else
            {
                object obj = objs[0];
                FieldAttribute fieldAttribute=(FieldAttribute)obj;
                fieldAttribute.FieldValue=field.GetValue(this,null);
                return fieldAttribute;
            }
        }
}
 
創(chuàng)建數(shù)據(jù)實(shí)體:

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

復(fù)制代碼 代碼如下:表名稱(chēng):Wincms_Dictionary
字段名稱(chēng):DictionaryId

希望本文所述對(duì)大家的.NET程序設(shè)計(jì)有所幫助。

AspNet技術(shù).net使用自定義類(lèi)屬性實(shí)例,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 一道本加勒比 | 精品成人资源在线观看 | 中文字幕天天躁日日躁狠狠躁97 | 97精品在线播放 | 免费韩国一级毛片 | 美女国产在线观看免费观看 | 色综合色狠狠天天综合色 | wwwxx欧美 | 国产精品七七在线播放 | 精品国产一区二区三区免费 | 韩国精品一区视频在线播放 | 91精品福利视频 | 国产99r视频精品免费观看 | 亚洲第一性网站 | 最色影院 | 55夜色66夜色国产精品站 | 国产观看精品一区二区三区 | 91精品综合国产在线观看 | 免费观看国产一区二区三区 | 2021最新国产成人精品视频 | 国产大片黄在线看免费 | 天天摸日日干 | 欧美日韩亚洲国产一区二区综合 | 美女视频黄频大全免费视频 | 国产一区二区在线看 | 婷婷色九月综合激情丁香 | 国产成人亚洲欧美激情 | 欧洲精品码一区二区三区免费看 | my blow jober act | 成人免费黄色网址 | 免费视频一区 | 精品综合一区二区三区 | 一本色道久久99一综合 | 久久伊人精品一区二区三区 | 亚洲精品综合 | 一级做a爰片久久毛片看看 一级做a爰片久久毛片毛片 | 国产成人久久蜜一区二区 | 黄在线 | 精品在线小视频 | 国产原创麻豆 | 成人午夜免费视频 |