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#通过反射获取类上的自定义特性的更多相关文章
- C# 通过反射获取方法/类上的自定义特性
1.所有自定义属性都必须继承System.Attribute 2.自定义属性的类名称必须为 XXXXAttribute 即是已Attribute结尾 自定义属性QuickWebApi [Attribu ...
- php反射获取类和方法中的注释
通过php中的反射机制,获取该类的文档注释,再通过获取其所有的方法,获取方法的注释 所用到的主要类及其方法 ReflectionClass ReflectionClass::getDocComment ...
- Java反射学习-1 - 反射获取类的属性,方法,构造器
新建一个Person类 package cn.tx.reflect; /** * 注解初步了解 * @author Administrator * */ public class Person { p ...
- java 通过反射获取类属性结构,类方法,类父类及其泛型,类,接口和包
首先自定义三个类 package reflection1; public interface MtInterface { void info(); } package reflection1; imp ...
- java利用反射获取类的属性及类型
java利用反射获取类的属性及类型. import java.lang.reflect.Field; import java.math.BigDecimal; import java.util.Map ...
- .NET C#利用反射获取类文件以及其中的方法&属性 并获取类及方法上的特性
了解C#特性类并声明我们自己的特性类[AttributeTest]代码如下 using System; namespace AttributeTest { /* 特性说明 特性本质是一个继承和使用了系 ...
- Java反射获取类和对象信息全解析
反射可以解决在编译时无法预知对象和类是属于那个类的,要根据程序运行时的信息才能知道该对象和类的信息的问题. 在两个人协作开发时,你只要知道对方的类名就可以进行初步的开发了. 获取类对象 Class.f ...
- java反射-使用反射获取类的所有信息
在OOP(面向对象)语言中,最重要的一个概念就是:万事万物皆对象. 在java中,类也是一个对象,是java.lang.Class的实例对象,官网称该对象为类的类类型. Class 类的实例表示正在运 ...
- [转]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 { ...
随机推荐
- HDU 5475 An easy problem 线段树
An easy problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- git在windows下clone、pull或者push内存溢出的解决办法
发现国内使用git来真正管理源码的人不多,特别是大数据量的源码,今天使用git clone android的源码时突然出现remote out of memery,解决办法: git config - ...
- [RxJS] Subject basic
A Subject is a type that implements both Observer and Observable types. As an Observer, it can subsc ...
- SAP交货单过账自动生产采购订单、采购订单自动收货入库
公司间需要买卖操作,由于发货和收货都是同一批人在操作,为了减少业务人员的工作量,提高工作效率,特实现以上功能 1.增强实现:增强点为交货单过账成功时触发,在提交前触发,如果遇到不可预知问题,可能造成数 ...
- php测试代码工具类
<?php error_reporting (E_ALL); ini_set ('display_errors', 'on'); ?> <meta http-equiv=&quo ...
- Hibernate中SQLite方言
package com.lain.util; /** * 2013-7-25 * chongzhen_zhao * SQLite方言 */ import java.sql.Types; import ...
- Android之UI--重绘EditText以及实现Button的渐变色
在本文中实现的是比较普遍的一个对EditText的重绘以及对于按钮或窗口添加渐变色. 因为EditText是继承于TextView的,所以可以实现对EditText的重绘,在重绘的时候只需要继承Edi ...
- 使用angular的ng-repeat遇到的一个问题
问题描述:ng-repeat绑定的数据集动态更新之后其包裹的子元素所绑定的事件全部丢失:问题详述:问题代码如下: <style> img{width:100px;height:100px; ...
- 基于ubuntu和windows连接
对于ubuntu和centos安装软件是不一样的 对于ubuntu是 apt-get install +软件名字 但是对于centos是 yum install +软件名字 所以ubunu远程连接 ...
- android之TCP客户端框架
一.程序框架 1.1 创建方法 onCreate 1.1.1 创建连接按键线程,并使能线程(触发原因:可按键.其他操作,并进行状态判断): Connect_Thread connect_Thread ...