国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

c#通過(guò)反射獲取類上的自定義特性

2019-11-08 03:10:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

下面這個(gè)是筆者在以前的一個(gè)項(xiàng)目中用到的。當(dāng)時(shí)是為了在導(dǎo)出Excel報(bào)表的時(shí)侯,通過(guò)自定義特性,包含一些可配置的特性在里面。具體的操作excel不是本文重點(diǎn),本文不會(huì)多做說(shuō)明。下面只寫個(gè)示例,簡(jiǎn)單說(shuō)明一下如何通過(guò)反射獲取自定義特性。示例只在類和屬性上使用了自定義特性。讀者可以按照實(shí)際的項(xiàng)目需求,合理使用自定義特性。

1、實(shí)現(xiàn)實(shí)體自定義特性,繼承自Attribute類

   /// <summary>    /// 自定義特性 屬性或者類可用  支持繼承    /// </summary>    [AttributeUsage(AttributeTargets.PRoperty | AttributeTargets.Class, Inherited = true)]    public class EnitityMappingAttribute : Attribute    {        private string tableName;        /// <summary>        /// 實(shí)體實(shí)際對(duì)應(yīng)的表名        /// </summary>        public string TableName        {            get { return tableName; }            set { tableName = value; }        }        private string columnName;        /// <summary>        /// 中文列名        /// </summary>        public string ColumnName        {            get { return columnName; }            set { columnName = value; }        }    }

注釋中我已經(jīng)寫的很清楚,自定義特性中的屬性一個(gè)是實(shí)體實(shí)際對(duì)應(yīng)的數(shù)據(jù)庫(kù)表名,一個(gè)是對(duì)應(yīng)的中文列名稱。2、在實(shí)體中使用自定義特性

 /// <summary>    /// 會(huì)員 ,實(shí)際的表名叫MemberInfo,并不是和實(shí)體名一致    /// </summary>    [EnitityMapping(TableName="MemberInfo")]     public class Member    {        private int id;        [EnitityMapping(ColumnName="關(guān)鍵字")]        public int Id        {            get { return id; }            set { id = value; }        }        private string userName;        [EnitityMapping(ColumnName = "會(huì)員注冊(cè)名")]        public string UserName        {            get { return userName; }            set { userName = value; }        }        private string realName;        [EnitityMapping(ColumnName = "會(huì)員真實(shí)名")]        public string RealName        {            get { return realName; }            set { realName = value; }        }        private bool isActive;        /// <summary>        /// 是否活躍  沒有附加自定義屬性        /// </summary>        public bool IsActive        {            get { return isActive; }            set { isActive = value; }        }    }

3、顯示自定義特性

   class Program    {        /// <summary>        /// 通過(guò)反射取自定義屬性        /// </summary>        /// <typeparam name="T"></typeparam>        private static void DisplaySelfAttribute<T>() where T:class ,new()        {            string tableName = string.Empty;            List<string> listColumnName = new List<string>();            Type objType = typeof(T);            //取屬性上的自定義特性            foreach (PropertyInfo propInfo in objType.GetProperties())            {                object[] objAttrs = propInfo.GetCustomAttributes(typeof(EnitityMappingAttribute), true);                if (objAttrs.Length>0)                {                    EnitityMappingAttribute attr = objAttrs[0] as EnitityMappingAttribute;                    if (attr != null)                    {                        listColumnName.Add(attr.ColumnName); //列名                    }                }            }            //取類上的自定義特性            object[] objs = objType.GetCustomAttributes(typeof(EnitityMappingAttribute), true);            foreach (object obj in objs)            {                EnitityMappingAttribute attr = obj as EnitityMappingAttribute;                if (attr != null)                {                    tableName = attr.TableName;//表名只有獲取一次                    break;                }            }            if (string.IsNullOrEmpty(tableName))            {                tableName = objType.Name;            }            Console.WriteLine(string.Format("The tablename of the entity is:{0} ", tableName));            if (listColumnName.Count > 0)            {                Console.WriteLine("The columns of the table are as follows:");                foreach (string item in listColumnName)                {                    Console.WriteLine(item);                }            }        }        static void Main(string[] args)        {            DisplaySelfAttribute<Member>(); //顯示結(jié)果            Console.ReadLine();        }    }ps:在獲取自定義特性的地方,其實(shí)就是利用了GetCustomAttributes方法,這個(gè)沒什么好說(shuō)的。在實(shí)際開發(fā)的時(shí)候,通過(guò)反射的特性可以省卻我們很多繁瑣的事情,真像那句話說(shuō)的,“反射反射,程序員的快樂”。不過(guò),反射的性能問題還是需要格外注意的,比如,今天上午看到老趙的“Attribute操作的性能優(yōu)化方式”才發(fā)現(xiàn)原來(lái)還有那么多內(nèi)涵。  
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永胜县| 寻甸| 随州市| 肃南| 白水县| 漾濞| 荔波县| 疏勒县| 海丰县| 峡江县| 和政县| 连城县| 鲁甸县| 枣强县| 龙里县| 拜城县| 庆元县| 宜昌市| 九龙城区| 澎湖县| 伊金霍洛旗| 洮南市| 宁海县| 东平县| 桦南县| 广平县| 且末县| 凯里市| 奉贤区| 萨嘎县| 龙井市| 玉屏| 诏安县| 内丘县| 井研县| 台北县| 隆子县| 禄劝| 祁连县| 梁河县| 黑水县|