1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Reflection;
 6 
 7 namespace ConsoleApplication1
 8 {
 9     class Program
     {
         static void Main(string[] args)
         {
             //Operation op = new Operation();
             MethodInfo method = typeof(Operation).GetMethod("Add");
             Attribute[] atts = Attribute.GetCustomAttributes(method);
             foreach (Attribute att in atts)
             {
                 if (att.GetType() == typeof(CommandAttribute))
                 {
                     Console.WriteLine(((CommandAttribute)att).Name + "," + ((CommandAttribute)att).Label);
                 }
             }
             Console.ReadLine();
             return;
 
             #region 获取所有的方法属性
 
             Operation testClass = new Operation();
             Type type = testClass.GetType();
             // Iterate through all the methods of the class.
             foreach (MethodInfo mInfo in type.GetMethods())
             {
                 // Iterate through all the Attributes for each method.
                 foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo))
                 {
                     // Check for the AnimalType attribute.
                     if (attr.GetType() == typeof(CommandAttribute))
                         Console.WriteLine(
                             "Method {0} has a CommandAttribute {1},{2} .",
                             mInfo.Name, ((CommandAttribute)attr).Label, ((CommandAttribute)attr).Name);
                 }
             }
 
             #endregion
 
 
 
             Console.ReadLine();
         }
     }
 
 
 
 
     public class Operation
     {
         [Command("AddLabel", "AddName")]
         public void Add()
         {
             Console.WriteLine("Add");
         }
 
         [Command("DelLabel", "DelName")]
         public void Del()
         {
             Console.WriteLine("Del");
         }
     }
 
     [AttributeUsage(AttributeTargets.Method)]
     public class CommandAttribute : Attribute
     {
         public string Label { get; set; }
         public string Name { get; set; }
 
         public CommandAttribute() { }
 
         public CommandAttribute(string label, string name)
         {
             this.Label = label;
             this.Name = name;
         }
     }
 }

C# Attribute的用法的更多相关文章

  1. 有关C#标签Attribute的熟悉

    Attribute 简单用法: 最近用到了,所以静下心来找些资料看了一下,终于把这东西搞清楚了. 一.什么是Attribute 先看下面的三段代码: 1.自定义Attribute类:VersionAt ...

  2. [CLR via C#]18. Attribute

    attribute可以说是Microsoft .NET Framework提出的最具创意的技术之一了.利用attribute,可以声明性的为自己的代码构造添加注解,从而实现一些特殊的功能.attrib ...

  3. Attribute与元数据

    在MSDN中,Attribute被定义为“是被指定给某一声明的一则附加的声明性信息”. 我们可以通过Attribute来定义设计层面的信息以及运行时(run-time)信息,也可以利用Attribut ...

  4. 深入浅出Attribute(三)

    约定: 1.”attribute”和”attributes”均不翻译 2.”property”译为“属性” 3.msdn中的原句不翻译 4.”program entity”译为”语言元素” Attri ...

  5. css三类选择器 用法 引用

    css(层叠样式表): css用法:选择符{样式属性:取值;...} css选择器的分类: ①:标签选择器,such as:p{attribute:value;},p为标签选择器的name,该页面中所 ...

  6. JS中property与attribute的区别

    property与attirbute都是属性的意思,在JS中很容易混淆,但实际上二者有很大的区别.简单来说, property:是DOM中的属性,是JavaScript中的对像 attribute:是 ...

  7. 流行的JavaScript库 ——jQuery

    1.为了简化 JavaScript 的开发, 一些 JavsScript 库诞生了. JavaScript 库封装了很多预定义的对象和实用函数.能帮助使用者建立有高难度交互的 Web2.0 特性的富客 ...

  8. Linux驱动学习之常用的模块操作命令

    1.常用的模块操作命令 (1)lsmod(list module,将模块列表显示),功能是打印出当前内核中已经安装的模块列表 (2)insmod(install module,安装模块),功能是向当前 ...

  9. [转载]JavaEE学习篇之——JQuery技术详解

    原文链接:http://blog.csdn.net/jiangwei0910410003/article/details/32102187 1.简介2.工具3.jQuery对象 1.DOM对象转化成j ...

随机推荐

  1. PageViewController和UIPageControl

    以前经常把这两个东西当成一回事, PageViewController像电子书那样,一页之中可以放几个childViewcontroller,  然后左右翻,当前frame显示几个viewcontro ...

  2. http链接

    1,打开一个网页是多少个请求:如果是一个html页面就是一个,解析后面css,js就是很多个  一般都是多个http请求 浏览器先根据地址栏地址发送一个http请求获取html网页 然后浏览器解析ht ...

  3. Redis持久化的简单介绍

    Redis的强大功能很大程度上是由于其将所有数据都存储在内存中,为了使Redis在重启后仍能保证数据不丢失,需要将数据从内存中以某种形式持久化到硬盘中. Redis支持两种方式的持久化,一种是RDB方 ...

  4. LINUX下多路径的介绍和安装配置(测试未写完)

    一.什么是多路径 普通的电脑主机都是一个硬盘挂接到一个总线上,这里是一对一的关系.而到了有光纤组成的SAN环境,或者由iSCSI组成的IPSAN环境,由于主机和存储通过了光纤交换机或者多块网卡及IP来 ...

  5. dateframe行列插入和删除操作

    ar = np.array(list("ABCDEFG")) # array只是Convert,默认会copy源值.asarray也是Convert,如果源值是array则不cop ...

  6. Django 的路由分配系统

    Django的路由系统 URL配置(URL.conf)就像Django所支撑网站的目录,它的本质是URL与要为该URL调用的视图函数之间的映射表. 我们就是以这种方式告诉Django,遇到哪个URL的 ...

  7. Hibernate学习11——配置Hibernate二级缓存

    一.缓存的概念: 以空间换时间:   二.Hibernate缓存的分类: 前面我们讲的缓存都是session缓存:也叫一级缓存:get,load等缓存都是内置的,一级缓存: SessionFactor ...

  8. Python文件操作,with open as追加文本内容实例

    最常见的读写操作 import re with open('/Users/Mr.Long/Desktop/data.txt', 'w') as f: f.write('hello world') 就这 ...

  9. PorterDuff.Mode

    参考:http://weishu.me/2015/09/23/Xfermode-in-android/ Sa = Source alphaDa = Dest alphaSc = Source colo ...

  10. Window下安装Memecached

    原创,如有转载请注明来处! memcached是一套分布式的快取系统,当初是Danga Interactive为了LiveJournal所发展的,但被许多软件(如MediaWiki)所使用.这是一套开 ...