心如止水

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Attribute 类将预定义的系统信息或用户定义的自定义信息与目标元素相关联。目标元素可以是程序集、类、构造函数、委托、枚举、事件、字段、接口、方法、可移植可执行文件模块、参数、属性 (Property)、返回值、结构或其他属性 (Attribute)。
所有的自定义特性都要从Attribute类继承。
例如下面的例子,首先有一个Animal的枚举:

// An enumeration of animals. Start at 1 (0 = uninitialized).
    public enum Animal
    {
        
// Pets.
        Dog = 1,
        Cat,
        Bird,
    }

然后是一个自定义的特性,它含有一个Animal类型的信息,并暴露出来:
// A custom attribute to allow a target to have a pet.
    public class AnimalTypeAttribute : Attribute
    {
        
// The constructor is called when the attribute is set.
        public AnimalTypeAttribute(Animal pet)
        {
            thePet 
= pet;
        }

        
// Keep a variable internally 
        protected Animal thePet;

        
// .. and show a copy to the outside world.
        public Animal Pet
        {
            
get { return thePet; }
            
set { 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() { }
    }

最后我们可以在runtime的时候获得这些特性信息:

private void Form1_Load(object sender, EventArgs e)
        {
            AnimalTypeTestClass testClass 
= new AnimalTypeTestClass();
            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(AnimalTypeAttribute))
                        Console.WriteLine(
                            
"Method {0} has a pet {1} attribute.",
                            mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
                }

            }
        }

另外,

定义您自己的特性 (Attribute) 类时,可通过在特性 (Attribute) 类上放置 AttributeUsageAttribute 来控制特性 (Attribute)
类的使用方式。指示的特性 (Attribute) 类必须直接或间接地从Attribute 派生。

特性 (Attribute) 类具有定位参数和命名参数。特性 (Attribute) 类的每个公共构造函数为该类定义一个有效的定位参数
序列。命名参数则由特性 (Attribute) 类的非静态、公共和读写字段或属性 (Property) 定义。

通过定义以下参数设置 AttributeUsageAttribute 的三个属性 (Property):

  • ValidOn

该定位参数指定可在其上放置所指示的特性 (Attribute) 的程序元素。AttributeTargets 枚举数中列出了可在其
上放置特性 (Attribute) 的所有可能元素的集合。可通过按位“或”运算组合多个 AttributeTargets 值,以获取
所需的有效程序元素组合。

  • AllowMultiple

该命名参数指定能否为给定的程序元素多次指定所指示的特性。

  • Inherited

该命名参数表示这个特性能否被继承。

例如,针对public class AnimalTypeAttribute : Attribute特性,我们可以改成

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class AnimalTypeAttribute : Attribute
{   ... }

表示这个特性只能加在Method前,不能同时加多次,以及不能被继承。

posted on 2008-10-13 17:06  cutebear  阅读(430)  评论(0编辑  收藏  举报