C# 特性AttributeUsage簡介與使用教程
AttributeUsage
預定義特性AttributeUsage描述了如何使用一個自定義特性類。它規(guī)定了特性可應用到的項目的類型。
規(guī)定該特性的語法如下:
[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)]
含義為:定制特性類,不允許多次使用,不能被繼承
第一個參數(shù):
因為它的特性目標是 AttributeTargets.Class,而它放在函數(shù)前面,所以上面的程序會報錯:特性“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); } } }
第二個參數(shù):
因為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); } } }
第三個參數(shù):
因為Inherited = false,所以運行結果為;
如果把Inherited = false 改為 Inherited = true,效果如下:
到此這篇關于C# 特性AttributeUsage的理解與使用的文章就介紹到這了,更多相關C# AttributeUsage使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C#使用smtp發(fā)送帶附件的郵件實現(xiàn)方法
這篇文章主要介紹了C#使用smtp發(fā)送帶附件的郵件實現(xiàn)方法,可直接將string類型結果保存為附件,實例中備有相應的注釋便于理解,需要的朋友可以參考下2014-11-11C#開發(fā)Winform實現(xiàn)學生管理系統(tǒng)
這篇文章介紹了C#開發(fā)Winform實現(xiàn)學生管理系統(tǒng)的項目案例,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05