濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > .net使用自定義類(lèi)屬性實(shí)例

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

熱門(mén)標(biāo)簽:騰訊地圖標(biāo)注手機(jī) 400電話(huà)如何申請(qǐng)取消 昆明語(yǔ)音電銷(xiāo)機(jī)器人價(jià)格 柳州電銷(xiāo)機(jī)器人公司 征途美甲店地圖標(biāo)注 百度地圖怎樣做地圖標(biāo)注 電銷(xiāo)語(yǔ)音機(jī)器人型號(hào)參數(shù) 太原400電話(huà)上門(mén)辦理 浦發(fā)電話(huà)機(jī)器人提醒還款

一般來(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ì)有所幫助。

您可能感興趣的文章:
  • 淺談python類(lèi)屬性的訪問(wèn)、設(shè)置和刪除方法
  • PHP中類(lèi)屬性與類(lèi)靜態(tài)變量的訪問(wèn)方法示例
  • MyBatis學(xué)習(xí)教程(四)-如何快速解決字段名與實(shí)體類(lèi)屬性名不相同的沖突問(wèn)題
  • Python類(lèi)屬性與實(shí)例屬性用法分析
  • PowerShell中調(diào)用.NET對(duì)象的靜態(tài)方法、靜態(tài)屬性和類(lèi)方法、類(lèi)屬性例子
  • 從零學(xué)Python之引用和類(lèi)屬性的初步理解
  • JavaScript類(lèi)屬性的訪問(wèn)方式詳解
  • 在Struts2中如何將父類(lèi)屬性序列化為JSON格式的解決方法
  • python 基礎(chǔ)學(xué)習(xí)第二彈 類(lèi)屬性和實(shí)例屬性
  • Python類(lèi)屬性的延遲計(jì)算

標(biāo)簽:江蘇 白山 天門(mén) 陽(yáng)泉 張家界 新疆 德陽(yáng) 蘭州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《.net使用自定義類(lèi)屬性實(shí)例》,本文關(guān)鍵詞  .net,使用,自定義,類(lèi),屬性,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《.net使用自定義類(lèi)屬性實(shí)例》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于.net使用自定義類(lèi)屬性實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    比如县| 江西省| 金乡县| 阳城县| 宝山区| 富阳市| 岳西县| 巴林左旗| 西平县| 光山县| 丰台区| 当阳市| 乐亭县| 韶关市| 离岛区| 无锡市| 湄潭县| 鄄城县| 平果县| 弥勒县| 阿坝| 安溪县| 泰和县| 辽宁省| 石柱| 永和县| 青海省| 南宫市| 昌吉市| 台南县| 县级市| 永定县| 南投县| 淳化县| 荆州市| 漳浦县| 大邑县| 马鞍山市| 西昌市| 六枝特区| 大同市|