.net使用自定義類屬性實(shí)例
來源:易賢網(wǎng) 閱讀:713 次 日期:2014-10-16 10:31:21
溫馨提示:易賢網(wǎng)小編為您整理了“.net使用自定義類屬性實(shí)例”,方便廣大網(wǎng)友查閱!

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

下面以定義一個(gè)簡單數(shù)據(jù)庫表的映射實(shí)體類來說明相關(guān)的使用方法,基于自定義類屬性和自定義類中的屬性的自定義屬性,可以方便的進(jìn)行類標(biāo)記和類中屬性的標(biāo)記

創(chuàng)建一個(gè)類的自定義屬性,用于標(biāo)識數(shù)據(jù)庫中的表名稱,需要繼承自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; }

}

}

創(chuàng)建一個(gè)屬性的自定義屬性,用于標(biāo)識數(shù)據(jù)庫表中字段的名稱,需要繼承自Attribute類:

代碼如下:

[AttributeUsage(AttributeTargets.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 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;}

}

}

創(chuàng)建一個(gè)數(shù)據(jù)實(shí)體基類:

代碼如下:

public class BaseEntity

{

public BaseEntity()

{

}

/// <summary>

/// 獲取表名稱

/// </summary>

/// <returns></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)識TableAttribute屬性");

}

else

{

object obj = objs[0];

TableAttribute ta = (TableAttribute)obj;

return ta.TableName; //獲取表名稱

}

}

/// <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 = field.GetCustomAttributes(typeof(FieldAttribute), true);

if (objs.Length <= 0)

{

throw new Exception("類體屬性名" + propertyName + "沒有標(biāo)識FieldAttribute屬性");

}

else

{

object obj = objs[0];

FieldAttribute fieldAttribute=(FieldAttribute)obj;

fieldAttribute.FieldValue=field.GetValue(this,null);

return fieldAttribute;

}

}

}

創(chuàng)建數(shù)據(jù)實(shí)體:

代碼如下:

[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 = value;

}

}

}

///基于實(shí)體類獲取實(shí)體對應(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 fieldAttribute=entity.GetFieldAttribute(propertyName);

return fieldAttribute.FieldName;

}

}

輸出結(jié)果為:

代碼如下:

表名稱:Wincms_Dictionary

字段名稱:DictionaryId

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:.net使用自定義類屬性實(shí)例
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)