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 ...
随机推荐
- Quartz 2D编程指南(5) - 变换(Transforms)
Quartz 2D 绘制模型定义了两种独立的坐标空间:用户空间(用于表现文档页)和设备空间(用于表现设备的本地分辨率).用户坐标空间用浮点数表示坐标,与设备空间的像素分辨率没有关系.当我们需要一个点或 ...
- vector 中的clear()
为什么clear之后,还是输出fdsafdsa.有什么办法可以真正清空之? 因为对于vector,clear并不真正释放内存(这是为优化效率所做的事),clear实际所做的是为vector中所保存的所 ...
- mysql update select 用法
之前用SqlServer , update语句对表进行更新:update a set a.xx= (select yy from b) ; 是可以的但是在mysql中,不能直接使用set select ...
- LeetCode Word Abbreviation
原题链接在这里:https://leetcode.com/problems/word-abbreviation/description/ 题目: Given an array of n distinc ...
- VBA遍历单元格
Sub test() Dim str Dim i, j i = j = ).UsedRange.Rows.Count ).UsedRange.Columns.Count ).Cells(r, c).V ...
- 几种分布式调用技术的比较 -- RPC VS REST
我之前在传统IT公司干活,后来来了互联网,感受到了很多不同,其中有一点就是两者使用到的技术有一些差别.比如说分布式调用技术. 我在的这家公司内部的服务架构是基于Thrift的,服务基于Thrift进行 ...
- 使用cookie纪录访问次数
由于是简单的demo,我就没有链接数据库,退出重新登陆访问次数清零,只能靠下刷新来维持下访问次数 把用户名和次数初始化放进cookie Cookie uesrnameCookie = new Cook ...
- 嵌入式视频采集编程思路(Video 4 Linux)-转
转自:http://zyg0227.blog.51cto.com/1043164/271954 1. linux 内核有video for linux简称V4L.V4L是Linux影像系统与嵌入式影 ...
- coding style 的两点
通俗介绍coding style两点建议: 模块划分 这个如果做得不好,简直不能忍.有的代码非常莫名其妙,有些东西本身不复杂,非要将其拆成好几个部分,然后做成一个一个碎散的模块,这样并不好.举个例子, ...
- Window下安装Memecached
原创,如有转载请注明来处! memcached是一套分布式的快取系统,当初是Danga Interactive为了LiveJournal所发展的,但被许多软件(如MediaWiki)所使用.这是一套开 ...