欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

自定義類屬性怎么在.net項目中使用-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān)自定義類屬性怎么在.net項目中使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比工布江達(dá)網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式工布江達(dá)網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋工布江達(dá)地區(qū)。費用合理售后完善,十年實體公司更值得信賴。

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

復(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)建一個屬性的自定義屬性,用于標(biāo)識數(shù)據(jù)庫表中字段的名稱,需要繼承自Attribute類:

復(fù)制代碼 代碼如下:

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public class FieldAttribute : Attribute
{
        private readonly string _FieldName = "";    ///數(shù)據(jù)庫的字段名稱
        private System.Data.DbType _Type = System.Data.DbType.String;   ///數(shù)據(jù)庫的字段類型
 
        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)建一個數(shù)據(jù)實體基類:

復(fù)制代碼 代碼如下:

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("實體類沒有標(biāo)識TableAttribute屬性");
            }
            else
            {
                object obj = objs[0];
                TableAttribute ta = (TableAttribute)obj;
                return ta.TableName;                            //獲取表名稱
            }
        }
        /// <summary>
        /// 獲取數(shù)據(jù)實體類上的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 + "沒有標(biāo)識FieldAttribute屬性");
            }
            else
            {
                object obj = objs[0];
                FieldAttribute fieldAttribute=(FieldAttribute)obj;
                fieldAttribute.FieldValue=field.GetValue(this,null);
                return fieldAttribute;
            }
        }
}


 
創(chuàng)建數(shù)據(jù)實體:

復(fù)制代碼 代碼如下:

[Table("Wincms_Dictionary")]            ///映射到數(shù)據(jù)庫的Wincms_Dictionary表
public class Wincms_Dictionary : BaseEntity
{
         private int _DictionaryId;
 
         public Wincms_Dictionary()
         {
         }
 
        [Field("DictionaryId",DbType.Int32)]                ///映射到數(shù)據(jù)庫的Wincms_Dictionary表中的字段
        public int DictionaryId
        {
            get { return this._DictionaryId; }
            set
            {
                this._DictionaryId = value;
            }
        }
}
 
///基于實體類獲取實體對應(yīng)的表名稱和字段名稱
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;
       }
}


輸出結(jié)果為:

復(fù)制代碼 代碼如下:

表名稱:Wincms_Dictionary
字段名稱:DictionaryId

看完上述內(nèi)容,你們對自定義類屬性怎么在.net項目中使用有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

文章題目:自定義類屬性怎么在.net項目中使用-創(chuàng)新互聯(lián)
標(biāo)題來源:http://www.aaarwkj.com/article20/cocsco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供GoogleApp開發(fā)、定制開發(fā)小程序開發(fā)、外貿(mào)網(wǎng)站建設(shè)、用戶體驗

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)
日本高清久久一区二区三区| 国产欧美日韩国产欧美日| 国产精品国产三级国产av丨| 风流少妇奶真白摸的好爽| 精品视频中文字幕天码| 秒播视频午夜福利在线观看| 黑人巨大精品欧美久久| 91大片在线观看视频| 亚洲人成伊人成综合网中文| 91麻豆视频福利视频| 正在播放蜜臀av在线| 欧美日韩国产综合一区二区| 日韩三级视频一区二区| 中文字幕乱码人妻一区| 首页亚洲一区二区三区| 美女性生活免费视频网站| 在线国产一区二区不卡| 在线免费观看视频97| 亚洲精品一区二区三区pp| 亚洲日本中文字幕免费观看| 国产精品国产三级国av中文 | av天堂久久这里只有精品美国| 亚洲成人av综合在线| 午夜在线免费观看小视频| 亚洲精品国产第一区| 放荡精品少妇一区二区三区| 黑人巨大精品欧美黑寡妇| 中文在线在线天堂中文| 亚洲小视频免费在线观看| 粉嫩极品国产在线观看| 国产精品欧美一区二区视频| 久久99久久精品视频国产| 欧美日韩国产综合一区二区| 日本五十路亲子在线一区| 精品熟妇人妻一区二区三区| 国产精品一级片一区二区| 成熟人妻一区二区三区人妻| 日韩av网址在线免费观看| 中文字幕乱码一区二区欧美| 日韩视频看看色网站| 无套内射精品一区二区|