zoukankan      html  css  js  c++  java
  • 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
    10     {
    11         static void Main(string[] args)
    12         {
    13             //Operation op = new Operation();
    14             MethodInfo method = typeof(Operation).GetMethod("Add");
    15             Attribute[] atts = Attribute.GetCustomAttributes(method);
    16             foreach (Attribute att in atts)
    17             {
    18                 if (att.GetType() == typeof(CommandAttribute))
    19                 {
    20                     Console.WriteLine(((CommandAttribute)att).Name + "," + ((CommandAttribute)att).Label);
    21                 }
    22             }
    23             Console.ReadLine();
    24             return;
    25 
    26             #region 获取所有的方法属性
    27 
    28             Operation testClass = new Operation();
    29             Type type = testClass.GetType();
    30             // Iterate through all the methods of the class.
    31             foreach (MethodInfo mInfo in type.GetMethods())
    32             {
    33                 // Iterate through all the Attributes for each method.
    34                 foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo))
    35                 {
    36                     // Check for the AnimalType attribute.
    37                     if (attr.GetType() == typeof(CommandAttribute))
    38                         Console.WriteLine(
    39                             "Method {0} has a CommandAttribute {1},{2} .",
    40                             mInfo.Name, ((CommandAttribute)attr).Label, ((CommandAttribute)attr).Name);
    41                 }
    42             }
    43 
    44             #endregion
    45 
    46 
    47 
    48             Console.ReadLine();
    49         }
    50     }
    51 
    52 
    53 
    54 
    55     public class Operation
    56     {
    57         [Command("AddLabel""AddName")]
    58         public void Add()
    59         {
    60             Console.WriteLine("Add");
    61         }
    62 
    63         [Command("DelLabel""DelName")]
    64         public void Del()
    65         {
    66             Console.WriteLine("Del");
    67         }
    68     }
    69 
    70     [AttributeUsage(AttributeTargets.Method)]
    71     public class CommandAttribute : Attribute
    72     {
    73         public string Label { getset; }
    74         public string Name { getset; }
    75 
    76         public CommandAttribute() { }
    77 
    78         public CommandAttribute(string label, string name)
    79         {
    80             this.Label = label;
    81             this.Name = name;
    82         }
    83     }
    84 }
  • 相关阅读:
    spring揭密学习笔记(3)-spring ioc容器:Spring的IoC容器之BeanFactory
    spring揭密学习笔记(3)-spring ioc容器:掌管大局的IoC Service Provider
    spring揭密学习笔记(2)-spring ioc容器:IOC的基本概念
    spring揭密学习笔记(1) --spring的由来
    spring揭密学习笔记
    spring事务管理实现原理-源码-传播属性
    spring事务传播实现源码分析
    IDEA搭建Spring框架环境
    ScrollView滑动的监听
    Android对apk源代码的改动--反编译+源代码改动+又一次打包+签名【附HelloWorld的改动实例】
  • 原文地址:https://www.cnblogs.com/pnljs/p/3610863.html
Copyright © 2011-2022 走看看