.net使用自定義類(lèi)屬性實(shí)例

字號(hào):


    一般來(lái)說(shuō),在.net中可以使用type.getcustomattributes獲取類(lèi)上的自定義屬性,可以使用propertyinfo.getcustomattributes獲取屬性信息上的自定義屬性。
    下面以定義一個(gè)簡(jiǎn)單數(shù)據(jù)庫(kù)表的映射實(shí)體類(lèi)來(lái)說(shuō)明相關(guān)的使用方法,基于自定義類(lèi)屬性和自定義類(lèi)中的屬性的自定義屬性,可以方便的進(jìn)行類(lèi)標(biāo)記和類(lèi)中屬性的標(biāo)記
    創(chuàng)建一個(gè)類(lèi)的自定義屬性,用于標(biāo)識(shí)數(shù)據(jù)庫(kù)中的表名稱(chēng),需要繼承自attribute類(lèi):
    代碼如下:
    [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ù)表中字段的名稱(chēng),需要繼承自attribute類(lèi):
    代碼如下:
    [attributeusage(attributetargets.property, inherited = false, allowmultiple = false)]
    public class fieldattribute : attribute
    {
    private readonly string _fieldname = ; ///數(shù)據(jù)庫(kù)的字段名稱(chēng)
    private system.data.dbtype _type = system.data.dbtype.string; ///數(shù)據(jù)庫(kù)的字段類(lèi)型
    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í)體基類(lèi):
    代碼如下:
    public class baseentity
    {
    public baseentity()
    {
    }
    /// <summary>
    /// 獲取表名稱(chēng)
    /// </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í)體類(lèi)沒(méi)有標(biāo)識(shí)tableattribute屬性);
    }
    else
    {
    object obj = objs[0];
    tableattribute ta = (tableattribute)obj;
    return ta.tablename; //獲取表名稱(chēng)
    }
    }
    /// <summary>
    /// 獲取數(shù)據(jù)實(shí)體類(lèi)上的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(類(lèi)體屬性名 + 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í)體:
    代碼如下:
    [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í)體類(lèi)獲取實(shí)體對(duì)應(yīng)的表名稱(chēng)和字段名稱(chēng)
    public class test
    {
    public static void main(string[] args)
    {
    wincms_dictionary dict=new wincms_dictionary();
    console.writeline(表名稱(chēng):+gettablename(dict));
    console.writeline(字段名稱(chēng):+getfieldname(dict,dictionaryid));
    console.read();
    }
    ///獲取實(shí)體表名稱(chēng)
    public static string gettablename(baseentity entity)
    {
    return entity.gettablename();
    }
    ///獲取實(shí)體字段名稱(chēng)
    public static string getfieldname(baseentity entity,string propertyname)
    {
    fieldattribute fieldattribute=entity.getfieldattribute(propertyname);
    return fieldattribute.fieldname;
    }
    }
    輸出結(jié)果為:
    代碼如下:
    表名稱(chēng):wincms_dictionary
    字段名稱(chēng):dictionaryid