C# 通过反射获取方法/类上的自定义特性
1.所有自定义属性都必须继承System.Attribute
2.自定义属性的类名称必须为 XXXXAttribute 即是已Attribute结尾
自定义属性QuickWebApi
- [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
- public class QuickWebApiAttribute: Attribute
- {
- public QuickWebApiAttribute(string _serviceCode,string _controllerName,string _actionName)
- {
- ServicesCode = _serviceCode;
- ControllerName = _controllerName;
- ActionName = _actionName;
- }
- public string ServicesCode{ get; set; }
- public string ControllerName { get; set; }
- public string ActionName { get; set; }
- }
接口类
- public interface ISchool
- {
- [QuickWebApi("SchoolServices", "School", "GetSchoolAll")]
- List<School> GetSchoolAll();
- [QuickWebApi("SchoolServices", "School", "GetSchoolListById")]
- List<School> GetSchoolListById(string schoolId);
- }
具体实现
- static void Main(string[] args)
- {
- Type t = typeof(ISchool);
- //获取方法特性中ActionName为GetSchoolAll的特性
- var b = t.GetMethods().Single(p => CustomAttributeExtensions.GetCustomAttribute<QuickWebApiAttribute>(p).ActionName == "GetSchoolAll");
- QuickWebApiAttribute qu= CustomAttributeExtensions.GetCustomAttribute<QuickWebApiAttribute>(b);
- //循环遍历
- foreach (MemberInfo p in t.GetMethods())
- {
- object[] Attribute1 = p.GetCustomAttributes(true);
- object[] Attribute2 = p.GetCustomAttributes(typeof(QuickWebApiAttribute), false);
- //获取遍历结果
- //QuickWebApiAttribute att = CustomAttributeExtensions.GetCustomAttribute<QuickWebApiAttribute>(p);
- string a = "";
- }
- Console.ReadKey();
- }
知识扩展
- public enum AttributeTargets
- {
- // 摘要:
- // 可以对程序集应用特性。
- Assembly = ,
- //
- // 摘要:
- // 可以对模块应用特性。
- Module = ,
- //
- // 摘要:
- // 可以对类应用特性。
- Class = ,
- //
- // 摘要:
- // 可以对结构应用特性,即值类型。
- Struct = ,
- //
- // 摘要:
- // 可以对枚举应用特性。
- Enum = ,
- //
- // 摘要:
- // 可以对构造函数应用特性。
- Constructor = ,
- //
- // 摘要:
- // 可以对方法应用特性。
- Method = ,
- //
- // 摘要:
- // 可以对属性应用特性。
- Property = ,
- //
- // 摘要:
- // 可以对字段应用特性。
- Field = ,
- //
- // 摘要:
- // 可以对事件应用特性。
- Event = ,
- //
- // 摘要:
- // 可以对接口应用特性。
- Interface = ,
- //
- // 摘要:
- // 可以对参数应用特性。
- Parameter = ,
- //
- // 摘要:
- // 可以对委托应用特性。
- Delegate = ,
- //
- // 摘要:
- // 可以对返回值应用特性。
- ReturnValue = ,
- //
- // 摘要:
- // 可以对泛型参数应用特性。
- GenericParameter = ,
- //
- // 摘要:
- // 可以对任何应用程序元素应用特性。
- All = ,
- }
C# 通过反射获取方法/类上的自定义特性的更多相关文章
- c#通过反射获取类上的自定义特性
c#通过反射获取类上的自定义特性 本文转载:http://www.cnblogs.com/jeffwongishandsome/archive/2009/11/18/1602825.html 下面这个 ...
- C#提高--------------获取方法返回值的自定义特性(Attribute)
.NET(C#):获取方法返回值的自定义特性(Attribute) 转载 2013年05月08日 10:54:42 1456 来自:http://www.cnblogs.com/mgen/archiv ...
- java中的反射机制,以及如何通过反射获取一个类的构造方法 ,成员变量,方法,详细。。
首先先说一下类的加载,流程.只有明确了类这个对象的存在才可以更好的理解反射的原因,以及反射的机制. 一. 类的加载 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三 ...
- <经验杂谈>C#中一种最简单、最基本的反射(Reflection):通过反射获取方法函数
说起反射之前和很多用C#/.net的同仁们一样,相比于一般应用层对数据的增删改查总有点觉得深奥到难以理解.其实程序这东西,用过.实践过就很简单,我一直这么认为. 先说下概念:反射 Reflection ...
- Java实现通过反射获取指定类的所有信息
package com.ljy; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.l ...
- org.reflections 接口通过反射获取实现类源码研究
org.reflections 接口通过反射获取实现类源码研究 版本 org.reflections reflections 0.9.12 Reflections通过扫描classpath,索引元数据 ...
- 获取枚举值上的Description特性说明
/// <summary> /// 获取枚举值上的Description特性说明 /// </summary> /// <typeparam name="T&q ...
- java-通过反射获取目标类的属性,方法,构造器
首先定义一个urse package com.studay_fanshe; public class User { private String uname; private int age; pri ...
- 通过反射获取方法的参数名称(JDK8以上支持)
方法的参数名,在很多时候我们是需要反射得到的.但是在java8之前,代码编译为class文件后,方法参数的类型是固定的,但参数名称却丢失了,这和动态语言严重依赖参数名称形成了鲜明对比.(java是静态 ...
随机推荐
- vue + echarts画圈圈
<div class="chart-bar-left" id= "chartbar-left" style="margin-top:1%;&qu ...
- 对象初始化的完整过程(C#)
1.静态构造函数 在引入本文的主题之前,我们先来铺垫一下吧,看看静态构造函数的概念及用途. C#中允许创建无参数构造函数,该函数仅执行一次.它一般被用来初始化静态字段.CLR不能保证在某个特定时刻执行 ...
- c# 线程的基本使用
创建线程 线程的基本操作 线程和其它常见的类一样,有着很多属性和方法,参考下表: 创建线程的方法有很多种,这里我们先从thread开始创建线程 class Program { static void ...
- PHP创建与解析 XML 1 (36)
一.使用SimpleXML操控XML 要处理XML 文件,有两种传统的处理思路:SAX 和DOM.SAX 基于事件触发机制,对XML 文件进行一次扫描,完成要进行的处理:DOM 则将整个XML 文件构 ...
- 关于CentOS-6的默认带的mysql启动和安装问题
http://blog.csdn.net/arrowzz/article/details/24439731 以下纯复制粘贴: 一开始想自己一步一步从编译开始搭建一个lanmp环境: 从鸟哥的linux ...
- Linux 服务器加入Windows AD
背景信息: Windows AD Version: Windows Server 2012 R2 zh-cn 计算机全名:hlm12r2n1.hlm.com 域:hlm.com 域控管理员:stone ...
- Java中常见的jar包及其主要用途
jar包 用途 axis.jar SOAP引擎包 commons-discovery-0.2.jar 用来发现.查找和实现可插入式接口,提供一些一般类实例化.单件的生命周 ...
- Vue项目开发目录结构
最近做一些CI+Vue开发的简单项目,由于刚开始一直从事后端,对Vue了解不是很多,fork了团队的项目代码发现项目的文件夹很多,一时间有些懵,上网上参考了部分资料,博客园中的放晴的天空与狮子爱吃草两 ...
- ZigZag编码
ZigZag编码 在网络传输和数据存储场景中,需要对数据进行压缩.数据压缩的算法非常多,但大部分的数据压缩算法的原理是通过某种编码方式不存储数据中的0比特位,因此0比特位越多,数据压缩的效果越好.Zi ...
- 【转载】TableLayout表格布局详解
原文地址:http://www.cnblogs.com/manuosex/p/3584701.html 一.Tablelayout简介 Tablelayout类以行和列的形式对控件进行管理,每一行为一 ...