C# Atrribute特性

来源:互联网 发布:尼龙是什么材料 知乎 编辑:程序博客网 时间:2024/05/14 02:52
1、什么是Attribute
 Attribute类可以把目标元素和一个预定义的信息或者是用户自定义信息关联起来。这里的目标元素可以是assembly,class,constructor,delegate,enum,event,field,interface,method,可执行文件模块,parameter,property,return value,struct或其它的Attribute。Attribute提供的信息也被称为元数据(metadata)。元数据能用于在运行时控制怎样访问你的程序数据,或者在运行前通过额外的工具来控制怎样处理你的程序或部署它。例如.NET Framework预定义并使用attribute去控制运行时行为,一些编程语言使用attribute类型来描述.NET Framework中通用类型不直接支持的语言特性。所有的Attribute类型直接或间接从Attribute类继承。Attribute能应用到任何target元素;多个Attribute能应用到相同的元素;Attribute类提供遍历的方法去取出和测试自定义Attribute。更多关于Attribute的信息,可以看Applying Attributes和Extending Metadata Using Attributes。

2、定义或控制特性的使用
对于一个自定义的Attribute,你可以通过AttributeUsage的Attribute来限定你的Attribute 所施加的元素的类型。代码形式如下: 
[AttriubteUsage(参数设置)] public 自定义Attribute : Attribute { ... }
作为参数的AttributeTarges的值允许通过“或”操作来进行多个值得组合,如果你没有指定参数,那么默认参数就是All 。 AttributeUsage除了继承Attribute 的方法和属性之外,还定义了以下三个属性:
AllowMultiple: 读取或者设置这个属性,表示是否可以对一个程序元素施加多个Attribute 。
Inherited:读取或者设置这个属性,表示是否施加的Attribute 可以被派生类继承或者重载。
ValidOn: 读取或者设置这个属性,指明Attribute 可以被施加的元素的类型。

3、Attribute类

三个静态方法:

static Attribute GetCustomAttribute():这个方法有8种重载的版本,它被用来取出施加在类成员上指定类型的Attribute。

static Attribute[] GetCustomAttributes(): 这个方法有16种重载版本,用来取出施加在类成员上指定类型的Attribute数组。

static bool IsDefined():由八种重载版本,看是否指定类型的定制attribute被施加到类的成员上面。


4、定义一个Attriubute类
using UnityEngine;using System.Collections;using System.Collections.Generic;[System.AttributeUsage(System.AttributeTargets.Class,AllowMultiple=true)]public class DynamicSpriteAttribute : System.Attribute{    public List<string> Sprites = new List<string>();    public DynamicSpriteAttribute(params string[] sprites)     {        Sprites.AddRange(sprites);    }}
a、params允许变参数的输入
b、必须继承Attribute类
c、定义个成员变量用来存储或者访问数据

5、定义一个方法获取attribute特性
static string[] getDynamicSprites()    {        List<string> SpriteNames = new List<string>();             //  var asms = AppDomain.CurrentDomain.GetAssemblies ();     //   foreach (var asm in asms)        {            Assembly asm = Assembly.GetAssembly(typeof(RankItem)); //rankitem是一个当前程序集中的类,随便选一个当前程序集的类即可            Type[] typeList = asm.GetTypes();                       //获取asm程序集里面所有的类            for (int i = 0; i < typeList.Length; i++)            {                Type tp = typeList[i];                if (tp == typeof(RankItem))                {                    int k = 212122;                }                Attribute[] attri = Attribute.GetCustomAttributes(tp, true);    //获取类里面的所有attribute特性                for (int j = 0; j < attri.Length; j++)                {                    if (attri[j] is DynamicSpriteAttribute)             //判断你所需要的特性                    {                        DynamicSpriteAttribute dySprite = attri[j] as DynamicSpriteAttribute;                        SpriteNames.AddRange(dySprite.Sprites);                    }                }            }        }        return SpriteNames.ToArray();    }



6、使用特性类
在需要类的前面加上 [DynamicSpriteAttribute("Rank_1", "Rank_2", "Rank_3")]
这里传递的是三个字符串,因为特性类DynamicSpriteAttribute的构造函数是传递可变参数的字符串,也就是可以传递不定数量的字符串

7、还能干什么?
Attribute能干的事情太多了,比如你写了个类,想做ORM映射,里面有些字段不想映射到表,有些想映射到表。有些字段可能名字和表的字段不一样。这时候你就可以通过Attribute来标识哪个字段需要被映射,映射到数据库哪个字段。等等。做过网络框架的同学也应该比较熟悉的一个应用,使用Attribute来做自动的消息派发。总之,Attribute可以做很多自动化的事情,就看你怎么用了。

0 0