.net用法自定義類屬性實(shí)例__第1頁(yè)
.net用法自定義類屬性實(shí)例__第2頁(yè)
.net用法自定義類屬性實(shí)例__第3頁(yè)
.net用法自定義類屬性實(shí)例__第4頁(yè)
.net用法自定義類屬性實(shí)例__第5頁(yè)
已閱讀5頁(yè),還剩4頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論