C# Attribute的用法
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的用法的更多相关文章
- 有关C#标签Attribute的熟悉
Attribute 简单用法: 最近用到了,所以静下心来找些资料看了一下,终于把这东西搞清楚了. 一.什么是Attribute 先看下面的三段代码: 1.自定义Attribute类:VersionAt ...
- [CLR via C#]18. Attribute
attribute可以说是Microsoft .NET Framework提出的最具创意的技术之一了.利用attribute,可以声明性的为自己的代码构造添加注解,从而实现一些特殊的功能.attrib ...
- Attribute与元数据
在MSDN中,Attribute被定义为“是被指定给某一声明的一则附加的声明性信息”. 我们可以通过Attribute来定义设计层面的信息以及运行时(run-time)信息,也可以利用Attribut ...
- 深入浅出Attribute(三)
约定: 1.”attribute”和”attributes”均不翻译 2.”property”译为“属性” 3.msdn中的原句不翻译 4.”program entity”译为”语言元素” Attri ...
- css三类选择器 用法 引用
css(层叠样式表): css用法:选择符{样式属性:取值;...} css选择器的分类: ①:标签选择器,such as:p{attribute:value;},p为标签选择器的name,该页面中所 ...
- JS中property与attribute的区别
property与attirbute都是属性的意思,在JS中很容易混淆,但实际上二者有很大的区别.简单来说, property:是DOM中的属性,是JavaScript中的对像 attribute:是 ...
- 流行的JavaScript库 ——jQuery
1.为了简化 JavaScript 的开发, 一些 JavsScript 库诞生了. JavaScript 库封装了很多预定义的对象和实用函数.能帮助使用者建立有高难度交互的 Web2.0 特性的富客 ...
- Linux驱动学习之常用的模块操作命令
1.常用的模块操作命令 (1)lsmod(list module,将模块列表显示),功能是打印出当前内核中已经安装的模块列表 (2)insmod(install module,安装模块),功能是向当前 ...
- [转载]JavaEE学习篇之——JQuery技术详解
原文链接:http://blog.csdn.net/jiangwei0910410003/article/details/32102187 1.简介2.工具3.jQuery对象 1.DOM对象转化成j ...
随机推荐
- java基础:eclipse编程不得不知道的技巧
如果你是位具有开发经丰富的工程师,在开发的过程中,你就会很强烈的要求快捷的编程.如何快捷编程,只有更加熟悉开发工具.那么eclipse是同样也有很多技巧.可以带着下面问题来阅读1.如何查找类相关信息? ...
- 重温CLR(十六) CLR寄宿和AppDomain
寄宿(hosting)使任何应用程序都能利用clr的功能.特别要指出的是,它使现有应用程序至少能部分使用托管代码编写.另外,寄宿还为应用程序提供了通过编程来进行自定义和扩展的能力. 允许可扩展性意味着 ...
- DML操纵语句
--在新增数据的时候,如果在表名之后没有跟 列名,那么values()必须写全--顺序必须不能改变,这个顺序就是表中列的顺序insert into dept values(70,'20','哈哈') ...
- 如何点焊过的镍片再次焊接到 PCBA 上?
如何将点焊过的镍片再次焊接到 PCBA 上? 在 PCBA 上贴了镍片再点焊,这样的制造工艺可以大大减少人工处理,提高生产通过率. 由于种种原因,有些机器可能有故障需要维修,而且电池又需要拆下来,才能 ...
- python运算符一些注意项
python运算符一些注意项 '/'浮点除,和'//'整除 单个'/'是浮点除,两个除号'//'是整除 整除也适用于浮点数.但是,用整除计算浮点除的结果只是在整除的结果上浮点化,比如3.6//2.1, ...
- 教你30分钟学会XAML
1.狂妄的WPF 相对传统的Windows图形编程,需要做很多复杂的工作,引用许多不同的API.例如:WinForm(带控件表单).GDI+(2D图形).DirectX API(3D图形)以及流媒体和 ...
- session 与 cookie 区别
一.Session的概念 Session 是存放在服务器端的,类似于Session结构来存放用户数据,当浏览器 第一次发送请求时,服务器自动生成了一个Session和一个Session ID用来唯一标 ...
- RK3288 查看时钟树
主控端可以通过指令查看时钟树,enable_cnt为1,表示时钟已使能. # cat d/clk/clk_summary cat d/clk/clk_summary clock enable_cnt ...
- kafka--通过python操作topic
修改 topic 的分区数 shiyanlou:bin/ $ ./kafka-topics.sh --zookeeper localhost:2181 --alter --topic mySendTo ...
- 前端性能优化:gzip压缩文件传输数据
一.文件压缩的好处 前端生产环境中将js.css.图片等文件进行压缩的好处显而易见,通过减少数据传输量减小传输时间,节省服务器网络带宽,提高前端性能. 二.http协议如何支持压缩文件的传输 1.浏览 ...