本篇文章給大家分享的是有關(guān)C# 中AttributeUsage如何使用,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
成都創(chuàng)新互聯(lián)長期為千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為塔什庫爾干塔吉克企業(yè)提供專業(yè)的成都網(wǎng)站制作、成都做網(wǎng)站,塔什庫爾干塔吉克網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
C# AttributeUsage的使用要明白:
AttributeUsage有三個屬性,我們可以把它放置在定制屬性前面。***個屬性是:
◆ValidOn
通過這個屬性,我們能夠定義定制特性應(yīng)該在何種程序?qū)嶓w前放置。一個屬性可以被放置的所有程序?qū)嶓w在AttributeTargets enumerator中列出。通過OR操作我們可以把若干個AttributeTargets值組合起來。
◆AllowMultiple
這個屬性標記了我們的定制特性能否被重復(fù)放置在同一個程序?qū)嶓w前多次。
◆Inherited
我們可以使用這個屬性來控制定制特性的繼承規(guī)則。它標記了我們的特性能否被繼承。
C# AttributeUsage的使用實例:
下面讓我們來做一些實際的東西。我們將會在剛才的Help特性前放置AttributeUsage特性以期待在它的幫助下控制Help特性的使用。
using System; [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; } } }
先讓我們來看一下AttributeTargets.Class。它規(guī)定了Help特性只能被放在class的前面。這也就意味著下面的代碼將會產(chǎn)生錯誤:
[Help("this is a do-nothing class")] public class AnyClass { [Help("this is a do-nothing method")] //error public void AnyMethod() { } }
編譯器報告錯誤如下:
AnyClass.cs: Attribute 'Help' is not valid on this declaration type. It is valid on 'class' declarations only.
我們可以使用AttributeTargets.All來允許Help特性被放置在任何程序?qū)嶓w前??赡艿闹凳牵?/p>
Assembly, Module, Class, Struct, Enum, Constructor, Method, Property, Field, Event, Interface, Parameter, Delegate, All = Assembly | Module | Class | Struct | Enum | Constructor | Method | Property | Field | Event | Interface | Parameter | Delegate, ClassMembers = Class | Struct | Enum | Constructor | Method | Property | Field | Event | Delegate | Interface )
下面考慮一下AllowMultiple = false。它規(guī)定了特性不能被重復(fù)放置多次。
[Help("this is a do-nothing class")] [Help("it contains a do-nothing method")] public class AnyClass { [Help("this is a do-nothing method")] //error public void AnyMethod() { } }
它產(chǎn)生了一個編譯期錯誤。
AnyClass.cs: Duplicate 'Help' attribute
Ok,現(xiàn)在我們來討論一下***的這個屬性。Inherited, 表明當特性被放置在一個基類上時,它能否被派生類所繼承。
[Help("BaseClass")] public class Base { } public class Derive : Base { }
C# AttributeUsage的使用會有四種可能的組合:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false ] [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false ] [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true ] [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true ]
C# AttributeUsage的使用***種情況:
如果我們查詢(Query)(稍后我們會看到如何在運行期查詢一個類的特性)Derive類,我們將會發(fā)現(xiàn)Help特性并不存在,因為inherited屬性被設(shè)置為false。
C# AttributeUsage的使用第二種情況:
和***種情況相同,因為inherited也被設(shè)置為false。
C# AttributeUsage的使用第三種情況:
為了解釋第三種和第四種情況,我們先來給派生類添加點代碼:
[Help("BaseClass")] public class Base { } [Help("DeriveClass")] public class Derive : Base { }
現(xiàn)在我們來查詢一下Help特性,我們只能得到派生類的屬性,因為inherited被設(shè)置為true,但是AllowMultiple卻被設(shè)置為false。因此基類的Help特性被派生類Help特性覆蓋了。
C# AttributeUsage的使用第四種情況:
在這里,我們將會發(fā)現(xiàn)派生類既有基類的Help特性,也有自己的Help特性,因為AllowMultiple被設(shè)置為true。
以上就是C# 中AttributeUsage如何使用,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站標題:C#中AttributeUsage如何使用
本文鏈接:http://chinadenli.net/article8/jiijop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站策劃、移動網(wǎng)站建設(shè)、網(wǎng)站營銷、定制開發(fā)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)