c#通过反射获取类上的自定义特性

c#通过反射获取类上的自定义特性

本文转载:http://www.cnblogs.com/jeffwongishandsome/archive/2009/11/18/1602825.html

下面这个是笔者在以前的一个项目中用到的。当时是为了在导出excel报表的时侯,通过自定义特性,包含一些可配置的特性在里面。具体的操作excel不是本文重点,本文不会多做说明。下面只写个示例,简单说明一下如何通过反射获取自定义特性。示例只在类和属性上使用了自定义特性。读者可以按照实际的项目需求,合理使用自定义特性。

1、实现实体自定义特性,继承自Attribute类


Code    ///<summary>     /// 自定义特性 属性或者类可用  支持继承     ///</summary>    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, Inherited =true)]     publicclass EnitityMappingAttribute : Attribute     {         privatestring tableName;         ///<summary>         /// 实体实际对应的表名         ///</summary>        publicstring TableName         {             get { return tableName; }             set { tableName = value; }         }
        privatestring columnName;         ///<summary>         /// 中文列名         ///</summary>        publicstring ColumnName         {             get { return columnName; }             set { columnName = value; }         }     }

注释中我已经写的很清楚,自定义特性中的属性一个是实体实际对应的数据库表名,一个是对应的中文列名称。 2、在实体中使用自定义特性


Code ///<summary>     /// 会员 ,实际的表名叫MemberInfo,并不是和实体名一致     ///</summary>    [EnitityMapping(TableName="MemberInfo")]      publicclass Member     {         privateint id;         [EnitityMapping(ColumnName="关键字")]         publicint Id         {             get { return id; }             set { id = value; }         }
        privatestring userName;         [EnitityMapping(ColumnName ="会员注册名")]         publicstring UserName         {             get { return userName; }             set { userName = value; }         }
        privatestring realName;         [EnitityMapping(ColumnName ="会员真实名")]         publicstring RealName         {             get { return realName; }             set { realName = value; }         }
        privatebool isActive;         ///<summary>         /// 是否活跃  没有附加自定义属性         ///</summary>        publicbool IsActive         {             get { return isActive; }             set { isActive = value; }         }     }

3、显示自定义特性


Code    class Program     {         ///<summary>         /// 通过反射取自定义属性         ///</summary>         ///<typeparam name="T"></typeparam>        privatestaticvoid 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>)                 {                     EnitityMappingAttribute attr = objAttrs[] 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 >)             {                 Console.WriteLine("The columns of the table are as follows:");                 foreach (string item in listColumnName)                 {                     Console.WriteLine(item);                 }             }         }
        staticvoid Main(string[] args)         {             DisplaySelfAttribute<Member>(); //显示结果            Console.ReadLine();         }     }
上一篇:PAT (Basic Level) 1002. 写出这个数 (20)


下一篇:一些常用的集合工具的代码块(缓慢更新XD)