




下載本文檔
版權(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; /基于實體類獵取實體對應的表名稱和字段名稱 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)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 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ài)系統(tǒng)研究方法探討試題及答案
- 2024年陪診師考試疾病預防試題及答案
- 2024陪診師考試心理支持試題及答案
- 去化樓層培訓
- 城市污染的成因與防治方法試題及答案
- 電子商務教師資格證應試策略及試題答案
- 深入學習監(jiān)理工程師試題及答案
- 黑龍江省七臺河市勃利縣小學2024-2025學年數(shù)學五下期末經(jīng)典試題含答案
- 黑龍江省佳木斯市同江市2025年四下數(shù)學期末學業(yè)質(zhì)量監(jiān)測試題含解析
- 黑龍江省雙鴨山市尖山區(qū)一中2024-2025學年高三下學期調(diào)研物理試題含解析
- 發(fā)動機機械-01.1cm5a4g63維修手冊
- 2022年撫順特殊鋼股份有限公司招聘筆試試題及答案解析
- 兒童抑郁量表CDI
- 幼兒數(shù)字1-100字帖練習
- 細胞生物學-7細胞信號轉(zhuǎn)導課件
- 心電監(jiān)護操作評分標準
- 攪拌站安全培訓試卷
- Q∕SY 02098-2018 施工作業(yè)用野營房
- 浙教版勞動五年級下冊 項目三 任務三 環(huán)保小車我來造 教案
- 隔離開關(guān)培訓課件
- 圖像融合技術(shù)中英文對照外文翻譯文獻
評論
0/150
提交評論