




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、.net用法自定義類屬性實例_ 一般來說,在.net中可以用法Type.GetCustomAttributes獵取類上的自定義屬性,可以用法PropertyInfo.GetCustomAttributes獵取屬性信息上的自定義屬性。 下面以定義一個簡潔數(shù)據(jù)庫表的映射實體類來說明相關(guān)的用法方法,基于自定義類屬性和自定義類中的屬性的自定義屬性,可以便利的進行類標記和類中屬性的標記 創(chuàng)建一個類的自定義屬性,用于標識數(shù)據(jù)庫中的表名稱,需要繼承自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)建一個屬性的自定義屬性,用于標識數(shù)據(jù)庫表中字段的名稱,需要繼承自Attribute類: 代碼如下: AttributeUsage(Attribut
3、eTargets.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 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)建一個數(shù)據(jù)實體基類: 代碼如下: 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(實體類沒有標識TableAttribute屬性); else object obj = objs0; TableAttribute ta = (TableAttribute)obj; return ta.TableName; /獵取表名稱 /
6、 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 = f
7、ield.GetCustomAttributes(typeof(FieldAttribute), true); if (objs.Length = 0) throw new Exception(類體屬性名 + propertyName + 沒有標識FieldAttribute屬性); else object obj = objs0; FieldAttribute fieldAttribute=(FieldAttribute)obj; fieldAttribute.FieldValue=field.GetValue(this,null); return fieldAttribute; 創(chuàng)建數(shù)據(jù)實
8、體: 代碼如下: 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
9、 = 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 fieldAtt
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 注冊會計師知識系統(tǒng)的全面架構(gòu)試題及答案
- 微生物檢驗成就展望試題及答案
- 證券從業(yè)資格證證券工具運用試題及答案
- 項目管理心理素質(zhì)試題及答案
- 2025年注冊會計師考試職業(yè)素養(yǎng)試題及答案
- 項目成果達成的試題及答案
- 2025年注冊會計師考試全景觀察試題及答案
- 青海省海南藏族自治州本年度(2025)小學一年級數(shù)學部編版隨堂測試((上下)學期)試卷及答案
- 注冊會計師職業(yè)倫理的核心理念與影響試題及答案
- 2025年知識更新的證券從業(yè)資格證試題及答案
- 【中職專用】《旅游心理學》(高教第二版)高考模擬卷7(解析版)
- 水利水電工程可行性研究報告編制規(guī)程
- 2025屆北京市海淀區(qū)北京57中高二物理第一學期期末檢測試題含解析
- 社工站2024年度工作總結(jié)與2025年項目計劃
- 初級制圖員職業(yè)鑒定考試題及答案
- GB/T 44543-2024預(yù)制混凝土節(jié)段拼裝用環(huán)氧膠粘劑
- 一元高次不等式的解法教學設(shè)計
- 《山谷回音真好聽》名師課件(簡譜)
- 2024公安機關(guān)人民警察高級執(zhí)法資格考試題(解析版)
- 2024年小學六年級數(shù)學考試練習試卷(試卷版)
- 高考英語核心詞匯1000個
評論
0/150
提交評論