gxc

永远不要认为有什么事情是理所当然的!

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

using System;
using System.Reflection;

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

    [AttributeUsage(AttributeTargets.Class,Inherited
=false,AllowMultiple=true)]
    
public class AnimalTypeAttribute : Attribute
    
{
        
protected Animal thePet;
        
public Animal Pet
        
{
            
get return thePet; }
            
set { thePet = Pet; }
        }


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

    }


    [AnimalType(Animal.Pig)]
    [AnimalType(Animal.Dog)]
    
class AnimalTypeTestClass
    
{
    }


    
class DemoClass
    
{
        
static void Main(string[] args)
        
{
            AnimalTypeTestClass testClass 
= new AnimalTypeTestClass();
            Type type 
= testClass.GetType();
            
//获取type所拥有的标签.type.GetCustomAttributes(typeof(AnimalTypeAttribute)
            AnimalTypeAttribute[] attr = (AnimalTypeAttribute[])type.GetCustomAttributes(typeof(AnimalTypeAttribute), false);

            
for (int i = 0; i < attr.Length; i++)
            
{
                Console.WriteLine(attr[i].Pet);
            }


            Console.Read();
        }

    }

}
posted on 2006-03-29 15:08  gxc  阅读(184)  评论(0编辑  收藏  举报