C# 特性AttributeUsage簡介與使用教程
AttributeUsage
預(yù)定義特性AttributeUsage描述了如何使用一個(gè)自定義特性類。它規(guī)定了特性可應(yīng)用到的項(xiàng)目的類型。
規(guī)定該特性的語法如下:
[AttributeUsage( ? ?validon, ? ?AllowMultiple=allowmultiple, ? ?Inherited=inherited )]
validon:自定義特性的對象,可以是類、方法、屬性等對象(默認(rèn)值是 AttributeTargets.All)
AllowMultiple:是否允許被多次使用(默認(rèn)值為false:單用的)
Inherited:是否可被派生類繼承(默認(rèn)值為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)]
含義為:定制特性類,不允許多次使用,不能被繼承
第一個(gè)參數(shù):
因?yàn)樗奶匦阅繕?biāo)是 AttributeTargets.Class,而它放在函數(shù)前面,所以上面的程序會(huì)報(bào)錯(cuò):特性“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); } } }
第二個(gè)參數(shù):
因?yàn)锳llowMultiple = false,上面多次使用,所以報(bào)錯(cuò) 重復(fù)的“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); } } }
第三個(gè)參數(shù):
因?yàn)镮nherited = false,所以運(yùn)行結(jié)果為;
如果把Inherited = false 改為 Inherited = true,效果如下:
到此這篇關(guān)于C# 特性AttributeUsage的理解與使用的文章就介紹到這了,更多相關(guān)C# AttributeUsage使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#簡易人機(jī)對抗“石頭剪刀布”游戲的實(shí)現(xiàn)
本文主要介紹了C#簡易人機(jī)對抗“石頭剪刀布”游戲的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05C#使用smtp發(fā)送帶附件的郵件實(shí)現(xiàn)方法
這篇文章主要介紹了C#使用smtp發(fā)送帶附件的郵件實(shí)現(xiàn)方法,可直接將string類型結(jié)果保存為附件,實(shí)例中備有相應(yīng)的注釋便于理解,需要的朋友可以參考下2014-11-11C#開發(fā)Winform實(shí)現(xiàn)學(xué)生管理系統(tǒng)
這篇文章介紹了C#開發(fā)Winform實(shí)現(xiàn)學(xué)生管理系統(tǒng)的項(xiàng)目案例,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05