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# 通过反射获取方法/类上的自定义特性的更多相关文章

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

    c#通过反射获取类上的自定义特性 本文转载:http://www.cnblogs.com/jeffwongishandsome/archive/2009/11/18/1602825.html 下面这个 ...

  2. C#提高--------------获取方法返回值的自定义特性(Attribute)

    .NET(C#):获取方法返回值的自定义特性(Attribute) 转载 2013年05月08日 10:54:42 1456 来自:http://www.cnblogs.com/mgen/archiv ...

  3. java中的反射机制,以及如何通过反射获取一个类的构造方法 ,成员变量,方法,详细。。

    首先先说一下类的加载,流程.只有明确了类这个对象的存在才可以更好的理解反射的原因,以及反射的机制. 一.  类的加载 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三 ...

  4. <经验杂谈>C#中一种最简单、最基本的反射(Reflection):通过反射获取方法函数

    说起反射之前和很多用C#/.net的同仁们一样,相比于一般应用层对数据的增删改查总有点觉得深奥到难以理解.其实程序这东西,用过.实践过就很简单,我一直这么认为. 先说下概念:反射 Reflection ...

  5. Java实现通过反射获取指定类的所有信息

    package com.ljy; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.l ...

  6. org.reflections 接口通过反射获取实现类源码研究

    org.reflections 接口通过反射获取实现类源码研究 版本 org.reflections reflections 0.9.12 Reflections通过扫描classpath,索引元数据 ...

  7. 获取枚举值上的Description特性说明

    /// <summary> /// 获取枚举值上的Description特性说明 /// </summary> /// <typeparam name="T&q ...

  8. java-通过反射获取目标类的属性,方法,构造器

    首先定义一个urse package com.studay_fanshe; public class User { private String uname; private int age; pri ...

  9. 通过反射获取方法的参数名称(JDK8以上支持)

    方法的参数名,在很多时候我们是需要反射得到的.但是在java8之前,代码编译为class文件后,方法参数的类型是固定的,但参数名称却丢失了,这和动态语言严重依赖参数名称形成了鲜明对比.(java是静态 ...

随机推荐

  1. C#不能捕捉的异常,如AccessViolationException

    在.net的异常机制中,有部分严重的编程错误(系统的某些Corrupted State Exceptions异常)是默认不被用户使用常规的异常捕捉方式捕捉到的. 微软的这种设计方式,是让用户必须处理该 ...

  2. python--类的约束,异常处理,MD5加密,日志处理logging模块

    1.类的约束 在开发中,如果项目经理需要对类进行约束,可以有两种方式 1. 对子类进行约束 Base: #对子类进行约束,必须重写这个方法 # 在工作中发现了NotImplementedError之后 ...

  3. python---基本数据类型 dict(字典)

    1. 什么是字典 字典是python中唯一的映射类型, 由{ } 括起来的键值对组成,在dict中key是唯一的.字典是以key:value的形式来保存数据, 字典存储数据的时候是用的hash值来存储 ...

  4. layui select 禁止点击

    $('select').attr('disabled', 'disabled'); form.render('select'); 注意事项: 1. 必须写 layui.use([form]) 2. 先 ...

  5. git log 与 git reflog 的 区别

    git log: commit 的版本日志 包含提交的版本 操作者 日期 (方便查看commit的版本,但是版本回退后,使用git log 看不到回退版本号之后的版本记录) commit ce0d69 ...

  6. robot framework学习笔记之一 资源文件(Resource)和外部资源(External Resources)

    一.资源文件(Resource) 测试套件主要是存放测试案例,资源文件主要是用来存放用户关键字. 添加资源    在目录型的Project/Test Suite下单击鼠标右键,选择『New Resou ...

  7. mysql 多实例

    linux系统下,先用mysql用户帐号的身份建立数据表:/usr/local/webserver/mysql/bin/mysql_install_db --basedir=/usr/local/we ...

  8. 【转载】Java 9 新特性——模块化

    来自 <http://www.jianshu.com/p/053a5ca89bbb#> 前言 年,我们将迎来 Java 语言的 22 岁生日,22岁,对于一个人而言,正是开始大展鸿图的年纪 ...

  9. 常用的re正则

    常用的正则表达式: 用户名:/^[a-z0-9_-]{3,16}$/ 密码:/^[a-z0-9_-]{6,18}$/ 十六进制值:/^#?([a-f0-9]{6}|[a-f0-9]{3})$/ 电子邮 ...

  10. [转] Mock以及Mockito的使用

    http://www.open-open.com/lib/view/open1462177583813.html [From] http://www.open-open.com/lib/view/op ...