惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
博客园_首页
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
P
Proofpoint News Feed
博客园 - 司徒正美
有赞技术团队
有赞技术团队
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog

博客园 - gxc

C#2.0中的泛型约束(转载) 解决‘“System.Configuration.ConfigurationSettings.AppSettings”已过时’的警告 《雷神之锤III》里求平方根倒数的函数 回溯法(vc)百鸡百钱问题 回溯法(vc)八皇后问题 六十六条经典禅语 prototype.js和Ajax 悖论 标签的使用(2) 自底向上的归并排序 自顶向下的归并排序 归并排序之归并算法 Josephus问题(循环链表) 找质数算法(Sieve of Eratosthenes筛法) 堆排序 直接选择排序 快速排序算法 Some of the new features from ASP.NET 2.0 在ASP.NET中使用AJAX
标签的使用(1)
gxc · 2006-03-29 · via 博客园 - gxc

using System;
using System.Reflection;

namespace CustomAttrCS
{
    
public enum Animal{Dog = 1,Cat,Bird,}

    
public class AnimalTypeAttribute : Attribute
    
{
        
protected Animal thePet;
        
public Animal Pet
        
{
            
get return thePet; }
            
set { thePet = Pet; }
        }


        
public AnimalTypeAttribute(Animal pet)
        
{
            thePet 
= pet;
        }

    }


    
// A test class where each method has its own pet. 
    class AnimalTypeTestClass
    
{
        [AnimalType(Animal.Dog)]
        
public void DogMethod() { }

        [AnimalType(Animal.Cat)]
        
public void CatMethod() { }

        [AnimalType(Animal.Bird)]
        
public void BirdMethod() { }
    }


    
class DemoClass
    
{
        
static void Main(string[] args)
        
{
            AnimalTypeTestClass testClass 
= new AnimalTypeTestClass();
            Type type 
= testClass.GetType();
            Console.WriteLine(type.ToString());
            
//循环type的所有方法
            foreach (MethodInfo mInfo in type.GetMethods())
            
{
                
int i = 0;
                Console.WriteLine(
"_____"+mInfo.ToString());
                
//循环每个方法的标签
                foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo))
                
{
                    i
++;
                    Console.WriteLine(
"11111111" + attr.ToString()+"******"+i.ToString());
                    
if (attr.GetType() == typeof(AnimalTypeAttribute))
                        Console.WriteLine(
"Method {0} has a pet {1} attribute.",mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
                }


            }

            Console.Read();
        }

    }

}