国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

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

2019-11-02 14:55:35
字體:
來源:轉載
供稿:網友

 一般來說,在.net中可以使用Type.GetCustomAttributes獲取類上的自定義屬性,可以使用PropertyInfo.GetCustomAttributes獲取屬性信息上的自定義屬性。

 
下面以定義一個簡單數據庫表的映射實體類來說明相關的使用方法,基于自定義類屬性和自定義類中的屬性的自定義屬性,可以方便的進行類標記和類中屬性的標記
 
創建一個類的自定義屬性,用于標識數據庫中的表名稱,需要繼承自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; }
        }
}

 

創建一個屬性的自定義屬性,用于標識數據庫表中字段的名稱,需要繼承自Attribute類:

 

 代碼如下:[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public class FieldAttribute : Attribute
{
        private readonly string _FieldName = "";    ///數據庫的字段名稱
        private System.Data.DbType _Type = System.Data.DbType.String;   ///數據庫的字段類型
 
        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;}
        }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吉木萨尔县| 托克托县| 通州市| 阳新县| 宝坻区| 沙坪坝区| 清水河县| 江西省| 建始县| 义马市| 青冈县| 凤台县| 石棉县| 龙川县| 玉环县| 当雄县| 上杭县| 盐山县| 房产| 三台县| 交城县| 武鸣县| 和硕县| 顺昌县| 龙江县| 济源市| 巴塘县| 禄丰县| 渭源县| 出国| 永州市| 鸡东县| 宁乡县| 东阿县| 多伦县| 高密市| 关岭| 岳西县| 黄山市| 安溪县| 揭东县|