C# 特性AttributeUsage簡介與使用教程
AttributeUsage
預定義特性AttributeUsage描述了如何使用一個自定義特性類。它規(guī)定了特性可應(yīng)用到的項目的類型。
規(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,所以運行結(jié)果為;

如果把Inherited = false 改為 Inherited = true,效果如下:

到此這篇關(guān)于C# 特性AttributeUsage的理解與使用的文章就介紹到這了,更多相關(guān)C# AttributeUsage使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用晚綁定來實現(xiàn)壓縮Access數(shù)據(jù)庫的方法
這篇文章主要介紹了C#使用晚綁定來實現(xiàn)壓縮Access數(shù)據(jù)庫的方法,項目開發(fā)中有一定的實用價值,需要的朋友可以參考下2014-08-08
C#通過XML節(jié)點屬性/屬性值讀取寫入XML操作代碼實例
本文主要介紹C#通過XML節(jié)點屬性、屬性值對XML的讀取,寫入操作,大家參考使用吧2013-11-11
.Net WInform開發(fā)筆記(五)關(guān)于事件Event
我前面幾篇博客中提到過.net中的事件與Windows事件的區(qū)別,本文討論的是前者,也就是我們代碼中經(jīng)常用到的Event,感興趣的朋友可以了解下2013-01-01
C#中一個高性能異步socket封裝庫的實現(xiàn)思路分享
下面小編就為大家分享一篇C#中一個高性能異步socket封裝庫的實現(xiàn)思路,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11
C# .Net實現(xiàn)灰度圖和HeatMap熱力圖winform(進階)
本文主要介紹了C# .NET實現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12

