版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、.net用法自定義類屬性實(shí)例_ 一般來說,在.net中可以用法Type.GetCustomAttributes獵取類上的自定義屬性,可以用法PropertyInfo.GetCustomAttributes獵取屬性信息上的自定義屬性。 下面以定義一個(gè)簡(jiǎn)潔數(shù)據(jù)庫(kù)表的映射實(shí)體類來說明相關(guān)的用法方法,基于自定義類屬性和自定義類中的屬性的自定義屬性,可以便利的進(jìn)行類標(biāo)記和類中屬性的標(biāo)記 創(chuàng)建一個(gè)類的自定義屬性,用于標(biāo)識(shí)數(shù)據(jù)庫(kù)中的表名稱,需要繼承自Attribute類: 代碼如下: AttributeUsage(AttributeTargets.Class, Inherited = false, Allo
2、wMultiple = 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ù)表中字段的名稱,需要繼承自Attribute類: 代碼如下: AttributeUsage(Attribut
3、eTargets.Property, Inherited = false, AllowMultiple = false) public class FieldAttribute : Attribute private readonly string _FieldName = ; /數(shù)據(jù)庫(kù)的字段名稱 private System.Data.DbType _Type = System.Data.DbType.String; /數(shù)據(jù)庫(kù)的字段類型 public FieldAttribute(string fieldName) this._FieldName=fieldName; public Fiel
4、dAttribute(string fieldName,System.Data.DbType type) this._FieldName=fieldName; this._Type=type; public string FieldName get return this._FieldName; public System.Data.DbType Type getreturn this._Type; 創(chuàng)建一個(gè)數(shù)據(jù)實(shí)體基類: 代碼如下: public class BaseEntity public BaseEntity() / summary / 獵取表名稱 / /summary / retur
5、ns/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)識(shí)TableAttribute屬性); else object obj = objs0; TableAttribute ta = (TableAttribute)obj; return ta.TableName; /獵取表名稱 /
6、 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 = f
7、ield.GetCustomAttributes(typeof(FieldAttribute), true); if (objs.Length = 0) throw new Exception(類體屬性名 + propertyName + 沒有標(biāo)識(shí)FieldAttribute屬性); else object obj = objs0; FieldAttribute fieldAttribute=(FieldAttribute)obj; fieldAttribute.FieldValue=field.GetValue(this,null); return fieldAttribute; 創(chuàng)建數(shù)據(jù)實(shí)
8、體: 代碼如下: 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
9、 = value; /基于實(shí)體類獵取實(shí)體對(duì)應(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 fieldAtt
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度會(huì)議翻譯及同聲傳譯服務(wù)合同范本
- 【培優(yōu)卷】同步分層練習(xí):四年級(jí)下冊(cè)語文第10課《綠》(含答案)
- 電信網(wǎng)絡(luò)安全防護(hù)與應(yīng)急處理機(jī)制
- 2025年度人工智能技術(shù)應(yīng)用合同模板
- 深度解析如何構(gòu)建成功的電子商務(wù)平臺(tái)-包括從戰(zhàn)略到執(zhí)行的全過程
- 2025年度有機(jī)蔬菜種子批發(fā)與種植技術(shù)支持合同
- 2025年度新型材料門窗研發(fā)與推廣合作協(xié)議
- 湘教版數(shù)學(xué)七年級(jí)上冊(cè)2.1《用字母表示數(shù)》聽評(píng)課記錄
- 2025年度新能源設(shè)備采購(gòu)與代理銷售服務(wù)協(xié)議
- 班級(jí)環(huán)境衛(wèi)生的教育意義與價(jià)值
- Bankart損傷的診療進(jìn)展培訓(xùn)課件
- 護(hù)理部用藥安全質(zhì)量評(píng)價(jià)標(biāo)準(zhǔn)
- 校園信息化設(shè)備管理檢查表
- 新版抗拔樁裂縫及強(qiáng)度驗(yàn)算計(jì)算表格(自動(dòng)版)
- API SPEC 5DP-2020鉆桿規(guī)范
- 創(chuàng)新思維課件(完整版)
- DB34∕T 4161-2022 全過程工程咨詢服務(wù)管理規(guī)程
- 部編版小學(xué)生語文教師:統(tǒng)編版語文1-6年級(jí)語文要素梳理
- 注塑成型工藝參數(shù)知識(shí)講解
- 安全生產(chǎn)專業(yè)化管理
- 初中生成長(zhǎng)檔案模板
評(píng)論
0/150
提交評(píng)論