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. Robot Framework-断言函数

    测试用例的目的是要验证一些操作否符合我们的预期结果,所以在测试用例中,断言函数是必不可少的一项.我们做的每一步操作都会有预期的结果,为了保证操作得到的结果符合预期,我们需要在测试用例中添加断言,来保证 ...

  2. WinForm窗体继承自定义的模板窗体出错

    在开发Winform程序的时候,我们往往需要根据需要做一些自定义的控件模块,这样可以给系统模块重复利用,或者实现更好的效果等功能.而今天自定义一个窗体,然后子窗体继承的时候出现了一点问题. 问题: 在 ...

  3. vbs命令1

    MsgBox "兄弟呀,你最近是否失眠",vbinformation Randomize() Dim a a=Int(rnd*2) Select Case a Case 0 Msg ...

  4. 一些IPC常用头文件

    //my_err.h#include <errno.h> /* for definition of errno */ #include <stdarg.h> /* ISO C ...

  5. TCP/IP概念简述

    这里所说的是广义上的TCP/IP协议群,而不是特指TCP和IP这两种具体的协议.既然是协议群,那么都有哪些协议呢?我们先不着急回答这个问题,因为要弄清楚这个问题,首先得了解另两件事,就是为啥要有这个协 ...

  6. WinForm界面布局控件WeifenLuo.WinFormsUI.Docking"的使用 (一)

    WinForm界面布局控件WeifenLuo.WinFormsUI.Docking"的使用 (一) 编写人:CC阿爸 2015-1-28 在伍华聪的博客中,看到布局控件"Weife ...

  7. Snippet取表字段说明和详细信息

    IF OBJECT_ID (N'dbo.GetDetails', N'IF') IS NOT NULL DROP FUNCTION dbo.GetDetails; GO create function ...

  8. JAVASE02-Unit012: Unit07: XML语法 、 XML解析

    Unit07: XML语法 . XML解析 emplist.xml <?xml version="1.0" encoding="UTF-8"?> & ...

  9. Google Cloud IAM中添加自定义域名

    Google Cloud支持在IAM中加入自定义域名.具体方法如下: 1. 登录Google Cloud Console,点击IAM和管理,进入身份和组织 2. 点击注册 3. 会重定向到Gmail到 ...

  10. ipvsadm的命令参考

    相信很多同学和我差不多,半桶水,貌似在配置lvs双机的时候,直接用的keepalived,ipvsadm就用来看看,感觉没啥用,今天无聊到处逛发现,某大神说,keepalived只是ipvsadm的一 ...