C#中关于Attribute的基本用法
来源:互联网 发布:ubuntu指令 编辑:程序博客网 时间:2024/05/22 01:36
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Reflection;namespace ConsoleApplication{ class Program { static void Main(string[] args) { 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 { [CommandAttribute("AddLabel", "AddName")] public void Add() { Console.WriteLine("Add"); } [CommandAttribute("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; } }}
0 0
- C#中关于Attribute的基本用法
- c#中特性的用法 Attribute (转)
- C#中关于@的用法
- C#中关于@的用法
- C#中关于@的用法
- C#中Attribute的应用
- C#中Attribute的应用
- 用javascript模拟C#的[Attribute]用法
- 用javascript模拟C#的[Attribute]用法
- C#中缓存的基本用法总结
- C#中缓存的基本用法总结
- c#中string的一些基本用法
- 关于C#中的Attribute的简单理解
- C# 关于Attribute的初级应用
- C#中关于using的用法
- 关于C#中list的用法
- 关于C#中Console的常见用法
- C#中Attribute和Property的区别
- leetcode104&110
- MARC与DC
- PG数据库之间的导入导出(pg_dumpall,pg_dump,psql,pg_restore)
- Android开发入门——推箱子游戏开发实战(七)
- 利用openstack建设适合中小型互联网企业的私有云(零)
- C#中关于Attribute的基本用法
- linux常用操作
- Codeforces Round #387 (Div. 2)B. Mammoth's Genome Decoding(水题)
- scala隐式转换implicit
- [Android]如何做一个崩溃率少于千分之三噶应用app--章节列表
- java.util.Date和java.sql.Date的区别和相互转化
- [一些网站]关于数据下载
- JVM调优:调优方法
- Performance Testing – Response vs. Latency vs. Throughput vs. Load vs. Scalability vs. Stress vs. Ro