亚洲免费乱码视频,日韩 欧美 国产 动漫 一区,97在线观看免费视频播国产,中文字幕亚洲图片

      1. <legend id="ppnor"></legend>

      2. 
        
        <sup id="ppnor"><input id="ppnor"></input></sup>
        <s id="ppnor"></s>

        .net使用自定義類屬性實例

        字號:


            一般來說,在.net中可以使用type.getcustomattributes獲取類上的自定義屬性,可以使用propertyinfo.getcustomattributes獲取屬性信息上的自定義屬性。
            下面以定義一個簡單數(shù)據(jù)庫表的映射實體類來說明相關(guān)的使用方法,基于自定義類屬性和自定義類中的屬性的自定義屬性,可以方便的進行類標(biāo)記和類中屬性的標(biāo)記
            創(chuàng)建一個類的自定義屬性,用于標(biāo)識數(shù)據(jù)庫中的表名稱,需要繼承自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)建一個屬性的自定義屬性,用于標(biāo)識數(shù)據(jù)庫表中字段的名稱,需要繼承自attribute類:
            代碼如下:
            [attributeusage(attributetargets.property, inherited = false, allowmultiple = false)]
            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 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(實體類沒有標(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ù)實體:
            代碼如下:
            [table(wincms_dictionary)] ///映射到數(shù)據(jù)庫的wincms_dictionary表
            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é)果為:
            代碼如下:
            表名稱:wincms_dictionary
            字段名稱:dictionaryid