午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

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

    一般來(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類:

    代碼如下:

    [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ù)專欄

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

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

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