C# 特性AttributeUsage的理解与使用
AttributeUsage
预定义特性 AttributeUsage 描述了如何使用一个自定义特性类。它规定了特性可应用到的项目的类型。
规定该特性的语法如下:
[AttributeUsage( validon, AllowMultiple=allowmultiple, Inherited=inherited )]
validon:自定义特性的对象,可以是类、方法、属性等对象(默认值是 AttributeTargets.All)
AllowMultiple:是否允许被多次使用(默认值为false:单用的)
Inherited:是否可被派生类继承(默认值为false:不能)
下面请看使用:
using System; namespace AttributeUsagePractice { [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] public class HelpAttribute : Attribute { public HelpAttribute(String Description_in) { this.description = Description_in; } protected String description; public String Description { get { return this.description; } } } class Program { [Help("this is a main class")] //error public static void Main(string[] args) { Console.WriteLine("Hello World!"); // TODO: Implement Functionality Here Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } } }
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
含义为:定制特性类,不允许多次使用,不能被继承
第一个参数:
因为它的特性目标是 AttributeTargets.Class,而它放在函数前面,所以上面的程序会报错:特性“Help”对此声明类型无效。它只对“class”声明有效,正确的做法是放在 class Program 上面。
如果是下面的代码:
using System; namespace AttributeUsagePractice { [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] public class HelpAttribute : Attribute { public HelpAttribute(String Description_in) { this.description = Description_in; } protected String description; public String Description { get { return this.description; } } } [Help("this is a main class")] [Help("this is a main2 class")] //error class Program { public static void Main(string[] args) { Console.WriteLine("Hello World!"); // TODO: Implement Functionality Here Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } } }
第二个参数:
因为AllowMultiple = false,上面多次使用,所以报错 重复的“Help”特性,正确的做法就是去掉它
using System; using System.Linq; namespace AttributeUsagePractice { [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] public class HelpAttribute : Attribute { public HelpAttribute(){} public HelpAttribute(String Description_in) { this.description = Description_in; } protected String description; public String Description { get { return this.description; } } } [Help("this is a HelpAttribute use class")] public class UseHelpAttribute { } public class UseHelpAttributeDerive : UseHelpAttribute { } class Program { public static void Main(string[] args) { // TODO: Implement Functionality Here UseHelpAttributeDerive objHelpAttribute = new UseHelpAttributeDerive(); Type t = objHelpAttribute.GetType(); object [] objAttrs = t.GetCustomAttributes(typeof(HelpAttribute),true); if(objAttrs!= null && objAttrs.Length > 0) { object temp = objAttrs.First(); HelpAttribute myAttr = temp as HelpAttribute; Console.WriteLine("类描述:{0}", myAttr.Description); } else { Console.WriteLine("没有类描述"); } Console.ReadKey(true); } } }
第三个参数:
因为Inherited = false,所以运行结果为;
如果把Inherited = false 改为 Inherited = true,效果如下:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2017-09-01 VS2010,First rebuild project is failed,Then build it again,Success!
2017-09-01 解决 Visual Studio _CRT_SECURE_NO_WARNINGS 警告