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();         }     }

c#通过反射获取类上的自定义特性的更多相关文章

  1. C# 通过反射获取方法/类上的自定义特性

    1.所有自定义属性都必须继承System.Attribute 2.自定义属性的类名称必须为 XXXXAttribute 即是已Attribute结尾 自定义属性QuickWebApi [Attribu ...

  2. php反射获取类和方法中的注释

    通过php中的反射机制,获取该类的文档注释,再通过获取其所有的方法,获取方法的注释 所用到的主要类及其方法 ReflectionClass ReflectionClass::getDocComment ...

  3. Java反射学习-1 - 反射获取类的属性,方法,构造器

    新建一个Person类 package cn.tx.reflect; /** * 注解初步了解 * @author Administrator * */ public class Person { p ...

  4. java 通过反射获取类属性结构,类方法,类父类及其泛型,类,接口和包

    首先自定义三个类 package reflection1; public interface MtInterface { void info(); } package reflection1; imp ...

  5. java利用反射获取类的属性及类型

    java利用反射获取类的属性及类型. import java.lang.reflect.Field; import java.math.BigDecimal; import java.util.Map ...

  6. .NET C#利用反射获取类文件以及其中的方法&属性 并获取类及方法上的特性

    了解C#特性类并声明我们自己的特性类[AttributeTest]代码如下 using System; namespace AttributeTest { /* 特性说明 特性本质是一个继承和使用了系 ...

  7. Java反射获取类和对象信息全解析

    反射可以解决在编译时无法预知对象和类是属于那个类的,要根据程序运行时的信息才能知道该对象和类的信息的问题. 在两个人协作开发时,你只要知道对方的类名就可以进行初步的开发了. 获取类对象 Class.f ...

  8. java反射-使用反射获取类的所有信息

    在OOP(面向对象)语言中,最重要的一个概念就是:万事万物皆对象. 在java中,类也是一个对象,是java.lang.Class的实例对象,官网称该对象为类的类类型. Class 类的实例表示正在运 ...

  9. [转]java 通过反射获取类的全局变量、方法、构造方法

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.str;   public class ZiFuChuan {       ...

随机推荐

  1. http://www.360doc.com/content/10/1012/09/3722251_60285817.shtml

    http://www.360doc.com/content/10/1012/09/3722251_60285817.shtml http://www.docin.com/p-163063250.htm ...

  2. BZOJ 2298: [HAOI2011]problem a 动态规划

    2298: [HAOI2011]problem a Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...

  3. Windows ICS 服务无法启动问题解决

    防火墙打不开肯定是"windows Firewall"服务没有启动.ICS服务启动不了能够通过下面方法解决: 1.找到本地连接,单击左键--属性--共享--勾选(√)--确定,如以 ...

  4. JavaScript网站设计实践(四)编写about.html页面,利用JavaScript和DOM,选择性的显示和隐藏DIV元素

    一.现在我们在网站设计(三)的基础上,来编写about.html页面. 这个页面要用到的知识点是利用JavaScript和DOM实现选择性地显示和隐藏某些DIV about.html页面在前面我们为了 ...

  5. Maven学习小结(三 基本概念)

    1.Maven POM POM(Project Object Model)项目对象模型,是用Maven来管理项目里的一个叫做pom.xml的文件.所有的项目配置信息都被定义在这个文件中, 通过这个文件 ...

  6. iOS 对网络视频采集视频截图

    在播放网络视频是  经常可以看到播放按钮下面是该制品的某个截图 : 一般情况下  后台服务器是可以把视频截图一起返回给你 你直接拿到图片显示就可以了 但是当后台没有提供时  我们也可以根据视频地址 自 ...

  7. Linux上安装Squall

    Squall是Storm之上的类SQL查询工具,能够将类SQL语句转换成topology,然后提交给Storm运行. 安装Squall前要先安装Java和sbt(simple build tool), ...

  8. huhamhire-hosts必备神器!

    huhamhire-hosts 不用说你就知道是干嘛用的. 上地址: https://github.com/huhamhire/huhamhire-hosts https://hosts.huhamh ...

  9. 将Word、Excel内容显示在Winform界面

    这里使用到dsoframer.ocx插件 1. 首先下载dsoframer.ocx插件,下载地址: http://pan.baidu.com/s/1kTKHeIj 2. 注册该插件 Win7的做法是按 ...

  10. 【转】BUG敏感度的培养

    在我们刚踏入软件测试行业时,不管你是专业的.非专业的,培训出来的还是未培训的.刚进公司时你看着身边的同时报的Bug很多并且大都是严重程度高,自己也很想提高一下,想要提高自己的bug敏感度,建议从下面几 ...