C# AttributeUsage
创始人
2024-05-28 09:45:41
0

C# AttributeUsage

预定义特性 AttributeUsage 描述了如何使用一个自定义特性类。它规定了特性可应用到的项目的类型。

规定该特性的语法如下:

[AttributeUsage(
validon,
AllowMultiple=allowmultiple,
Inherited=inherited
)]

validon:自定义特性的对象,可以是类、方法、属性等对象(默认值是 AttributeTargets.All)AttributeTargets 所有类型如下 可以使用 或运算符 |
AttributeTargets.All = AttributeTargets.Assembly | AttributeTargets.Module 等所有组合

    [Flags]public enum AttributeTargets{Assembly = 1,Module = 2,Class = 4,Struct = 8,Enum = 16,Constructor = 32,Method = 64,Property = 128,Field = 256,Event = 512,Interface = 1024,Parameter = 2048,Delegate = 4096,ReturnValue = 8192,GenericParameter = 16384,All = 32767}

AllowMultiple:是否允许被多次使用(默认值为false:单用的)
Inherited:是否可被派生类继承(默认值为false:不能)

定义如下

using System;// 定义一个 NpcAttribute,
// AttributeTargets.Class标记为类Class 使用
// AllowMultiple = true 可以多次使用
// Inherited = true 可以被继承/派生子类
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class NpcAttribute : Attribute
{public NpcAttribute(){}
}

使用如下

// 标记 NpcClass 类使用 NpcAttribute 属性
[Npc]
public class NpcClass
{
}
NpcClassExtend  继承 NpcClass
public class NpcClassExtend : NpcClass
{
}

标记了属性,如何使用、获取如下

private void NpcTest(){// 实例一个对象NpcClass npcClass = new NpcClass();// 获取对象类型Type t = npcClass.GetType();// 打印类型名Debug.LogError("t.Name:" + t.Name);// 获取 typeof(NpcAttribute) 属性, true 包含继承的object[] objAttrs = t.GetCustomAttributes(typeof(NpcAttribute), true);if (null != objAttrs && objAttrs.Length > 0){for (int i = 0; i < objAttrs.Length; ++i){object temp = objAttrs[i];// 类型转换NpcAttribute npcAttribute = temp as NpcAttribute;// 获取类型Debug.LogError("NpcClass 获取到 NpcAttribute:" + npcAttribute.ToString());}}else{Debug.LogError("NpcClass 未找到 NpcAttribute");}// 上面执行打印结果// t.Name:NpcClass// NpcClass 获取到 NpcAttribute:NpcAttribute}private void NpcExtendTest(){// 实例一个对象NpcClassExtend npcClassExtend = new NpcClassExtend();// 获取对象类型Type t = npcClassExtend.GetType();// 打印类型名Debug.LogError("t.Name:" + t.Name);// 获取 typeof(NpcAttribute) 属性, true 包含继承的object[] objAttrs = t.GetCustomAttributes(typeof(NpcAttribute), false);if (null != objAttrs && objAttrs.Length > 0){for (int i = 0; i < objAttrs.Length; ++i){object temp = objAttrs[i];// 类型转换NpcAttribute npcAttribute = temp as NpcAttribute;// 获取类型Debug.LogError("NpcClassExtend 获取到 NpcAttribute:" + npcAttribute.ToString());}}else{Debug.LogError("NpcClassExtend 未找到 NpcAttribute");}// 上面执行打印结果// t.Name:NpcClassExtend// NpcClassExtend 获取到 NpcAttribute:NpcAttribute// 如果上面代码修改,第二个参数 inherit 为 false// object[] objAttrs = t.GetCustomAttributes(typeof(NpcAttribute), false);// 执行打印结果// t.Name:NpcClassExtend// NpcClassExtend 未找到 NpcAttribute// 因为 NpcClass 标记了[Npc],NpcClassExtend 继承了 NpcClass// 获取时如果函数 GetCustomAttributes(Type attributeType, bool inherit)中 inherit 赋值为 false// 则 NpcClassExtend 无法获取到 NpcAttribute 属性}

修改

// Inherited = false 不允许派生、继承
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public class NpcAttribute : Attribute
{public NpcAttribute(){}
}

下面获取的 objAttrs 中是获取不到 NpcAttribute 的

 // 实例一个对象NpcClassExtend npcClassExtend = new NpcClassExtend();// 获取对象类型Type t = npcClassExtend.GetType();// 打印类型名Debug.LogError("t.Name:" + t.Name);// 获取 typeof(NpcAttribute) 属性, true 包含继承的object[] objAttrs = t.GetCustomAttributes(typeof(NpcAttribute), true);

AllowMultiple = true 作用,允许多次使用,代码如下

[Npc]
[Npc]
[Npc]
public class NpcClass
{}
    private void NpcTest(){// 实例一个对象NpcClass npcClass = new NpcClass();// 获取对象类型Type t = npcClass.GetType();// 打印类型名Debug.LogError("t.Name:" + t.Name);// 获取 typeof(NpcAttribute) 属性, true 包含继承的object[] objAttrs = t.GetCustomAttributes(typeof(NpcAttribute), true);if (null != objAttrs && objAttrs.Length > 0){for (int i = 0; i < objAttrs.Length; ++i){object temp = objAttrs[i];// 类型转换NpcAttribute npcAttribute = temp as NpcAttribute;// 获取类型Debug.LogError("NpcClass 获取到 NpcAttribute:" + npcAttribute.ToString());}}else{Debug.LogError("NpcClass 未找到 NpcAttribute");}// 上面执行打印结果// t.Name:NpcClass// NpcClass 获取到 NpcAttribute:NpcAttribute// NpcClass 获取到 NpcAttribute:NpcAttribute// NpcClass 获取到 NpcAttribute:NpcAttribute}

上面代码打印了三行 NpcClass 获取到 NpcAttribute:NpcAttribute,是因为NpcClass使用了三次标记[Npc]

修改代码,将 AllowMultiple 赋值为 false

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

则 NpcClass 报错,多次使用了 Npc 属性
Assets\Script\NpcClass.cs(6,2): error CS0579: Duplicate ‘Npc’ attribute

定义可传参数的属性,修改代码如下

using System;[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class NpcAttribute : Attribute
{private string _descript;public NpcAttribute(string descript){_descript = descript;}public string Descript{get { return _descript; }}
}[Npc("Npc 添加一次 ")]
[Npc("Npc 添加二次 ")]
[Npc("Npc 添加三次 ")]
public class NpcClass
{
}public class NpcClassExtend : NpcClass
{}private void NpcTest(){// 实例一个对象NpcClass npcClass = new NpcClass();// 获取对象类型Type t = npcClass.GetType();// 打印类型名Debug.LogError("t.Name:" + t.Name);// 获取 typeof(NpcAttribute) 属性, true 包含继承的object[] objAttrs = t.GetCustomAttributes(typeof(NpcAttribute), true);if (null != objAttrs && objAttrs.Length > 0){for (int i = 0; i < objAttrs.Length; ++i){object temp = objAttrs[i];// 类型转换NpcAttribute npcAttribute = temp as NpcAttribute;// 获取类型Debug.LogError("NpcClass 获取到 NpcAttribute:" + npcAttribute.ToString() + "   " + npcAttribute.Descript);}}else{Debug.LogError("NpcClass 未找到 NpcAttribute");}// 上面执行打印结果// t.Name:NpcClass// NpcClass 获取到 NpcAttribute:NpcAttribute   Npc 添加一次 // NpcClass 获取到 NpcAttribute:NpcAttribute   Npc 添加二次 // NpcClass 获取到 NpcAttribute:NpcAttribute   Npc 添加三次 }private void NpcExtendTest(){// 实例一个对象NpcClassExtend npcClassExtend = new NpcClassExtend();// 获取对象类型Type t = npcClassExtend.GetType();// 打印类型名Debug.LogError("t.Name:" + t.Name);// 获取 typeof(NpcAttribute) 属性, true 包含继承的object[] objAttrs = t.GetCustomAttributes(typeof(NpcAttribute), true);if (null != objAttrs && objAttrs.Length > 0){for (int i = 0; i < objAttrs.Length; ++i){object temp = objAttrs[i];// 类型转换NpcAttribute npcAttribute = temp as NpcAttribute;// 获取类型Debug.LogError("NpcClassExtend 获取到 NpcAttribute:" + npcAttribute.ToString() + "   " + npcAttribute.Descript);}}else{Debug.LogError("NpcClassExtend 未找到 NpcAttribute");}// 上面执行打印结果// t.Name:NpcClassExtend// NpcClassExtend 获取到 NpcAttribute:NpcAttribute   Npc 添加一次 // NpcClassExtend 获取到 NpcAttribute:NpcAttribute   Npc 添加二次 // NpcClassExtend 获取到 NpcAttribute:NpcAttribute   Npc 添加三次 }

相关内容

热门资讯

常用商务英语口语   商务英语是以适应职场生活的语言要求为目的,内容涉及到商务活动的方方面面。下面是小编收集的常用商务...
六年级上册英语第一单元练习题   一、根据要求写单词。  1.dry(反义词)__________________  2.writ...
复活节英文怎么说 复活节英文怎么说?复活节的英语翻译是什么?复活节:Easter;"Easter,anniversar...
2008年北京奥运会主题曲 2008年北京奥运会(第29届夏季奥林匹克运动会),2008年8月8日到2008年8月24日在中华人...
英语道歉信 英语道歉信15篇  在日常生活中,道歉信的使用频率越来越高,通过道歉信,我们可以更好地解释事情发生的...
六年级英语专题训练(连词成句... 六年级英语专题训练(连词成句30题)  1. have,playhouse,many,I,toy,i...
上班迟到情况说明英语   每个人都或多或少的迟到过那么几次,因为各种原因,可能生病,可能因为交通堵车,可能是因为天气冷,有...
小学英语教学论文 小学英语教学论文范文  引导语:英语教育一直都是每个家长所器重的,那么有关小学英语教学论文要怎么写呢...
英语口语学习必看的方法技巧 英语口语学习必看的方法技巧如何才能说流利的英语? 说外语时,我们主要应做到四件事:理解、回答、提问、...
四级英语作文选:Birth ... 四级英语作文范文选:Birth controlSince the Chinese Governmen...
金融专业英语面试自我介绍 金融专业英语面试自我介绍3篇  金融专业的学生面试时,面试官要求用英语做自我介绍该怎么说。下面是小编...
我的李老师走了四年级英语日记... 我的李老师走了四年级英语日记带翻译  我上了五个学期的小学却换了六任老师,李老师是带我们班最长的语文...
小学三年级英语日记带翻译捡玉... 小学三年级英语日记带翻译捡玉米  今天,我和妈妈去外婆家,外婆家有刚剥的`玉米棒上带有玉米籽,好大的...
七年级英语优秀教学设计 七年级英语优秀教学设计  作为一位兢兢业业的人民教师,常常要写一份优秀的教学设计,教学设计是把教学原...
我的英语老师作文 我的英语老师作文(通用21篇)  在日常生活或是工作学习中,大家都有写作文的经历,对作文很是熟悉吧,...
英语老师教学经验总结 英语老师教学经验总结(通用19篇)  总结是指社会团体、企业单位和个人对某一阶段的学习、工作或其完成...
初一英语暑假作业答案 初一英语暑假作业答案  英语练习一(基础训练)第一题1.D2.H3.E4.F5.I6.A7.J8.C...
大学生的英语演讲稿 大学生的英语演讲稿范文(精选10篇)  使用正确的写作思路书写演讲稿会更加事半功倍。在现实社会中,越...
VOA美国之音英语学习网址 VOA美国之音英语学习推荐网址 美国之音网站已经成为语言学习最重要的资源站点,在互联网上还有若干网站...
商务英语期末试卷 Part I Term Translation (20%)Section A: Translate ...