最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2014年10月10日 09:54:19   投稿:shichen2014  
這篇文章主要介紹了.net使用自定義類屬性實例,詳細講述了自定義類屬性的原理及實現(xiàn)方法,需要的朋友可以參考下

一般來說,在.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

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

相關(guān)文章

  • .Net中MoongoDB的簡單調(diào)用圖文教程

    .Net中MoongoDB的簡單調(diào)用圖文教程

    這篇文章主要給大家介紹了關(guān)于.Net中MoongoDB的簡單調(diào)用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • 值類型和引用類型的區(qū)別深入理解

    值類型和引用類型的區(qū)別深入理解

    值類型通常被分配在棧上,它的變量直接包含變量的實例,使用效率比較高;引用類型分配在托管堆上,引用類型的變量通常包含一個指向?qū)嵗闹羔槪兞客ㄟ^該指針來引用實例,需要的朋友可以了解下
    2012-12-12
  • .NET 線程基礎(chǔ)的使用介紹

    .NET 線程基礎(chǔ)的使用介紹

    本篇文章介紹了,.NET 線程基礎(chǔ)的使用說明,需要的朋友參考下
    2013-05-05
  • asp.net(C#)中給控件添加客戶端js事件的方法

    asp.net(C#)中給控件添加客戶端js事件的方法

    今天做一個輸入界面,有一需求根據(jù)一個DropDownList選擇不同,后面部分出現(xiàn)不同的輸入界面,若把響應(yīng)事件放在服務(wù)端去做,得頻繁刷頁面。就想放在客戶來處理顯示和隱藏相應(yīng)的輸入界面。
    2010-03-03
  • ASP.NET中使用AspnetAccessProvider

    ASP.NET中使用AspnetAccessProvider

    ASP.NET中使用AspnetAccessProvider...
    2007-09-09
  • Linux服務(wù)器下利用Docker部署.net Core項目的全過程

    Linux服務(wù)器下利用Docker部署.net Core項目的全過程

    這篇文章主要給大家介紹了關(guān)于在Linux服務(wù)器下利用Docker部署.net Core項目的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用.net Core具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • ASP.NET實現(xiàn)偽靜態(tài)網(wǎng)頁方法小結(jié)

    ASP.NET實現(xiàn)偽靜態(tài)網(wǎng)頁方法小結(jié)

    這篇文章主要介紹了ASP.NET實現(xiàn)偽靜態(tài)網(wǎng)頁方法小結(jié),主要包括了利用Httphandler實現(xiàn)URL重寫、地址重寫、利用Mircosoft URLRewriter.dll實現(xiàn)頁面?zhèn)戊o態(tài)等,需要的朋友可以參考下
    2014-09-09
  • ASP.NET百度Ueditor編輯器實現(xiàn)上傳圖片添加水印效果

    ASP.NET百度Ueditor編輯器實現(xiàn)上傳圖片添加水印效果

    這篇文章主要給大家介紹了ASP.NET百度Ueditor編輯器1.4.3這個版本實現(xiàn)上傳圖片添加水印效果的相關(guān)資料,文中通過圖文及示例代碼介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • ASP.NET中ListView(列表視圖)的使用前臺綁定附源碼

    ASP.NET中ListView(列表視圖)的使用前臺綁定附源碼

    ListView(列表視圖)想必大家都知道吧,接下來本文將介紹下ListView的使用前臺綁定,感興趣的你可不要錯過本文了哈
    2013-03-03
  • ASP.NET?Core獲取正確查詢字符串參數(shù)示例

    ASP.NET?Core獲取正確查詢字符串參數(shù)示例

    這篇文章主要為大家介紹了ASP.NET?Core正確獲取查詢字符串參數(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05

最新評論

光山县| 大石桥市| 衡东县| 遂平县| 宁夏| 兰考县| 淮阳县| 郎溪县| 万州区| 福泉市| 新巴尔虎右旗| 扶绥县| 泾阳县| 南汇区| 错那县| 丰原市| 新乡县| 金阳县| 静海县| 彩票| 仪征市| 嘉善县| 安庆市| 焦作市| 同江市| 正宁县| 延寿县| 高雄市| 工布江达县| 永州市| 宜黄县| 永年县| 新平| 玛曲县| 新郑市| 邢台县| 栾川县| 墨玉县| 宾川县| 重庆市| 扬州市|