.net使用自定義類屬性實例
一般來說,在.net中可以使用Type.GetCustomAttributes獲取類上的自定義屬性,可以使用PropertyInfo.GetCustomAttributes獲取屬性信息上的自定義屬性。
下面以定義一個簡單數(shù)據(jù)庫表的映射實體類來說明相關(guān)的使用方法,基于自定義類屬性和自定義類中的屬性的自定義屬性,可以方便的進行類標(biāo)記和類中屬性的標(biāo)記
創(chuàng)建一個類的自定義屬性,用于標(biāo)識數(shù)據(jù)庫中的表名稱,需要繼承自Attribute類:
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)建一個屬性的自定義屬性,用于標(biāo)識數(shù)據(jù)庫表中字段的名稱,需要繼承自Attribute類:
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)建一個數(shù)據(jù)實體基類:
{
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("實體類沒有標(biāo)識TableAttribute屬性");
}
else
{
object obj = objs[0];
TableAttribute ta = (TableAttribute)obj;
return ta.TableName; //獲取表名稱
}
}
/// <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 = 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ù)實體:
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;
}
}
}
///基于實體類獲取實體對應(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 fieldAttribute=entity.GetFieldAttribute(propertyName);
return fieldAttribute.FieldName;
}
}
輸出結(jié)果為:
字段名稱:DictionaryId
希望本文所述對大家的.net程序設(shè)計有所幫助。
- 淺談python類屬性的訪問、設(shè)置和刪除方法
- PHP中類屬性與類靜態(tài)變量的訪問方法示例
- MyBatis學(xué)習(xí)教程(四)-如何快速解決字段名與實體類屬性名不相同的沖突問題
- Python類屬性與實例屬性用法分析
- PowerShell中調(diào)用.NET對象的靜態(tài)方法、靜態(tài)屬性和類方法、類屬性例子
- 從零學(xué)Python之引用和類屬性的初步理解
- JavaScript類屬性的訪問方式詳解
- 在Struts2中如何將父類屬性序列化為JSON格式的解決方法
- python 基礎(chǔ)學(xué)習(xí)第二彈 類屬性和實例屬性
- Python類屬性的延遲計算
相關(guān)文章
ASP.NET泛型三之使用協(xié)變和逆變實現(xiàn)類型轉(zhuǎn)換
這篇文章介紹了ASP.NET使用協(xié)變和逆變實現(xiàn)泛型類型轉(zhuǎn)換的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08asp.net中動態(tài)改變網(wǎng)頁標(biāo)題的代碼
asp.net中動態(tài)改變網(wǎng)頁標(biāo)題的代碼,需要的朋友可以參考下。2011-02-02asp.net Web站點風(fēng)格切換的實現(xiàn)
Web站點的風(fēng)格切換是很常見、也很受大家歡迎的功能,比如大家熟知的博客園就提供了幾十款風(fēng)格模板供大家選擇。2009-05-05利用ASP.NET MVC和Bootstrap快速搭建個人博客之后臺dataTable數(shù)據(jù)列表
jQuery dataTables 插件是一個優(yōu)秀的表格插件,應(yīng)用非常廣泛,本文給大家介紹利用ASP.NET MVC和Bootstrap快速搭建個人博客之后臺dataTable數(shù)據(jù)列表,非常不錯,具有參考借鑒價值,感興趣的朋友一起看下吧2016-07-07利用委托把用戶控件的值顯示于網(wǎng)頁案例應(yīng)用
用戶控件(UserControl)是集成一個功能,需要處理好的數(shù)據(jù),然后存數(shù)據(jù)庫中并顯示于網(wǎng)頁上,讓用戶能檢測到處理的數(shù)據(jù)情況,接下來將介紹利用委托把用戶控件的值顯示于網(wǎng)頁上,感興趣的朋友可以了解下2013-02-02