下面以定義一個簡單數(shù)據(jù)庫表的映射實(shí)體類來說明相關(guān)的使用方法,基 " /> 亚洲高清在线天堂精品,一个人在线观看免费视频,日韩特黄特色大片免费视频

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

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

一般來說,在.NET中可以使用Type.GetCustomAttributes獲取類上的自定義屬性,可以使用PropertyInfo.GetCustomAttributes獲取屬性信息上的自定義屬性。
 
下面以定義一個簡單數(shù)據(jù)庫表的映射實(shí)體類來說明相關(guān)的使用方法,基于自定義類屬性和自定義類中的屬性的自定義屬性,可以方便的進(jì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ù)實(shí)體基類:

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

復(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;
            }
        }
}
 
///基于實(shí)體類獲取實(shí)體對應(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();
        }
 
       ///獲取實(shí)體表名稱
       public  static string GetTableName(BaseEntity entity)
       {
                return entity.GetTableName();
       }
 
       ///獲取實(shí)體字段名稱
       public static string GetFieldName(BaseEntity entity,string propertyName)
       {
              FieldAttribute fieldAttribute=entity.GetFieldAttribute(propertyName);
              return fieldAttribute.FieldName;
       }
}
輸出結(jié)果為:

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

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

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

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

主站蜘蛛池模板: 空姐内射出白浆10p 空姐厕所啪啪啪 | 纯肉无码AV在线看免费看 | 毛片无码免费无码播放 | 暖暖视频中国在线观看免费韩国 | 美女扒开屁股让男人桶 | 狠狠躁日日躁人人爽 | 日本一区不卡在线播放视频免费 | 久久精品美女 | 色婷婷国产精品视频一区二区三区 | 99热在线免费播放 | 日本伦理电影聚 | 国产午夜精品片一区二区三区 | 色AV色婷婷96人妻久久久 | ca88亚洲城娱乐 | 不卡一区二区高清观看视频 | 色翁荡熄月月 | 含羞草影院免费区 | 亚洲欧美偷拍视频一区 | 毛片免费观看的视频 | 热九九99香蕉精品品 | 久久偷拍人| 色狗综合网 | 欧美成人中文字幕在线看 | 欧美性xxx免费看片 欧美性xxx极品 | 99久久网站 | 欧美一区二区三区激情视频 | 中文字幕午夜乱理片 | japanese from色系| 和搜子的日子 在线观看 | 99久久热视频只有精品 | av色天堂2018在线观看 | 毛片免费观看 | 色老板美国在线观看 | 中文字幕日本在线mv视频精品 | 一区二区三区无码高清视频 | 国产一区二区在线免费观看 | 一本大道手机在线看 | 语文老师扒开胸罩喂我奶 | 国产成人精品永久免费视频 | 国产精品无码视频一区二区 | 欧美男女爱爱 |