一般來說,在.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, 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í)數(shù)據(jù)庫(kù)表中字段的名稱,需要繼承自attribute類:
代碼如下:
[attributeusage(attributetargets.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 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)識(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 + 沒有標(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í)體:
代碼如下:
[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 = 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é)果為:
代碼如下:
表名稱:wincms_dictionary
字段名稱:dictionaryid
更多信息請(qǐng)查看IT技術(shù)專欄