麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > .NET > 正文

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

2024-07-10 13:28:15
字體:
供稿:網(wǎng)友
這篇文章主要介紹了.net使用自定義類屬性實例,詳細講述了自定義類屬性的原理及實現(xiàn)方法,需要的朋友可以參考下
 
 

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

 

復制代碼代碼如下:
[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ù)實體基類:

 

 

復制代碼代碼如下:
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>
        /// 獲取數(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 + "沒有標識FieldAttribute屬性");
            }
            else
            {
                object obj = objs[0];
                FieldAttribute fieldAttribute=(FieldAttribute)obj;
                fieldAttribute.FieldValue=field.GetValue(this,null);
                return fieldAttribute;
            }
        }
}

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

 

 

復制代碼代碼如下:
[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;
            }
        }
}
 
///基于實體類獲取實體對應的表名稱和字段名稱
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é)果為:

 

 

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

 

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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 污污黄| 亚洲午夜久久久精品一区二区三区 | 日本在线免费观看视频 | 久久精品av| 午夜视频在线观看91 | 日韩毛片毛片久久精品 | 亚洲视频在线免费看 | 亚洲一区二区在线视频 | 91网页视频入口在线观看 | 国产精品刺激对白麻豆99 | 精品亚洲视频在线观看 | 亚洲第一成人在线观看 | 99热1| 麻豆传传媒久久久爱 | 久色乳综合思思在线视频 | 亚洲午夜激情网 | 成人午夜一区 | 国产精品一区二区手机在线观看 | 一区二区三区日韩 | 久久久久久片 | 成人免费毛片片v | 爱视频福利| 亚洲免费视频一区二区 | 一夜新娘第三季免费观看 | 国产韩国精品一区二区三区久久 | 久久成人黄色 | 91一级毛片 | 91麻豆精品国产91久久久点播时间 | 国产成人精品日本亚洲语音 | 日本a∨精品中文字幕在线 欧美1—12sexvideos | 久久精品亚洲欧美日韩精品中文字幕 | 欧美一级特级 | av在线播放亚洲 | 国产精品av久久久久久久久久 | 伊人在线 | 久久精品欧美视频 | 中文字幕在线观看91 | 日本一区二区视频在线观看 | 亚洲国产网站 | 亚洲日本韩国精品 | www.91操|