.net使用自定義類屬性實(shí)例
一般來(lái)說(shuō),在.net中可以使用Type.GetCustomAttributes獲取類上的自定義屬性,可以使用PropertyInfo.GetCustomAttributes獲取屬性信息上的自定義屬性。
下面以定義一個(gè)簡(jiǎn)單數(shù)據(jù)庫(kù)表的映射實(shí)體類來(lái)說(shuō)明相關(guān)的使用方法,基于自定義類屬性和自定義類中的屬性的自定義屬性,可以方便的進(jìn)行類標(biāo)記和類中屬性的標(biāo)記
創(chuàng)建一個(gè)類的自定義屬性,用于標(biāo)識(shí)數(shù)據(jù)庫(kù)中的表名稱,需要繼承自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)建一個(gè)屬性的自定義屬性,用于標(biāo)識(shí)數(shù)據(jù)庫(kù)表中字段的名稱,需要繼承自Attribute類:
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 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 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í)體類沒(méi)有標(biāo)識(shí)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 + "沒(méi)有標(biāo)識(shí)FieldAttribute屬性");
}
else
{
object obj = objs[0];
FieldAttribute fieldAttribute=(FieldAttribute)obj;
fieldAttribute.FieldValue=field.GetValue(this,null);
return fieldAttribute;
}
}
}
創(chuàng)建數(shù)據(jù)實(shí)體:
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 = 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 fieldAttribute=entity.GetFieldAttribute(propertyName);
return fieldAttribute.FieldName;
}
}
輸出結(jié)果為:
字段名稱:DictionaryId
希望本文所述對(duì)大家的.net程序設(shè)計(jì)有所幫助。
- 淺談python類屬性的訪問(wèn)、設(shè)置和刪除方法
- PHP中類屬性與類靜態(tài)變量的訪問(wèn)方法示例
- MyBatis學(xué)習(xí)教程(四)-如何快速解決字段名與實(shí)體類屬性名不相同的沖突問(wèn)題
- Python類屬性與實(shí)例屬性用法分析
- PowerShell中調(diào)用.NET對(duì)象的靜態(tài)方法、靜態(tài)屬性和類方法、類屬性例子
- 從零學(xué)Python之引用和類屬性的初步理解
- JavaScript類屬性的訪問(wèn)方式詳解
- 在Struts2中如何將父類屬性序列化為JSON格式的解決方法
- python 基礎(chǔ)學(xué)習(xí)第二彈 類屬性和實(shí)例屬性
- Python類屬性的延遲計(jì)算
相關(guān)文章
.Net中MoongoDB的簡(jiǎn)單調(diào)用圖文教程
這篇文章主要給大家介紹了關(guān)于.Net中MoongoDB的簡(jiǎn)單調(diào)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
ASP.NET中使用AspnetAccessProvider
ASP.NET中使用AspnetAccessProvider...2007-09-09
Linux服務(wù)器下利用Docker部署.net Core項(xiàng)目的全過(guò)程
這篇文章主要給大家介紹了關(guān)于在Linux服務(wù)器下利用Docker部署.net Core項(xiàng)目的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用.net Core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
ASP.NET實(shí)現(xiàn)偽靜態(tài)網(wǎng)頁(yè)方法小結(jié)
這篇文章主要介紹了ASP.NET實(shí)現(xiàn)偽靜態(tài)網(wǎng)頁(yè)方法小結(jié),主要包括了利用Httphandler實(shí)現(xiàn)URL重寫、地址重寫、利用Mircosoft URLRewriter.dll實(shí)現(xiàn)頁(yè)面?zhèn)戊o態(tài)等,需要的朋友可以參考下2014-09-09
ASP.NET百度Ueditor編輯器實(shí)現(xiàn)上傳圖片添加水印效果
這篇文章主要給大家介紹了ASP.NET百度Ueditor編輯器1.4.3這個(gè)版本實(shí)現(xiàn)上傳圖片添加水印效果的相關(guān)資料,文中通過(guò)圖文及示例代碼介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03
ASP.NET中ListView(列表視圖)的使用前臺(tái)綁定附源碼
ListView(列表視圖)想必大家都知道吧,接下來(lái)本文將介紹下ListView的使用前臺(tái)綁定,感興趣的你可不要錯(cuò)過(guò)本文了哈2013-03-03
ASP.NET?Core獲取正確查詢字符串參數(shù)示例
這篇文章主要為大家介紹了ASP.NET?Core正確獲取查詢字符串參數(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

