午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    C# 特性(Attribute)學(xué)習(xí)
    來源:易賢網(wǎng) 閱讀:901 次 日期:2015-03-23 15:49:19
    溫馨提示:易賢網(wǎng)小編為您整理了“C# 特性(Attribute)學(xué)習(xí)”,方便廣大網(wǎng)友查閱!

    特性(attribute)是被指定給某一聲明的一則附加的聲明性信息。

    在C#中,有一個(gè)小的預(yù)定義特性集合。在學(xué)習(xí)如何建立我們自己的定制特性(custom attributes)之前,我們先來看看在我們的代碼中如何使用預(yù)定義特性。

    using System;

    public class AnyClass

    {

    [Obsolete("Don't use Old method, use New method", true)]

    static void Old( ) { }

    static void New( ) { }

    public static void Main( )

    {

    Old( );

    }

    }

    我們先來看一下上面這個(gè)例子,在這個(gè)例子中我們使用了Obsolete特性,它標(biāo)記了一個(gè)不應(yīng)該再被使用的程序?qū)嶓w。第一個(gè)參數(shù)是一個(gè)字符串,它解釋了為什么該實(shí)體是過時(shí)的以及應(yīng)該用什么實(shí)體來代替它。實(shí)際上,你可以在這里寫任何文本。第二個(gè)參數(shù)告訴編譯器應(yīng)該把使用這個(gè)過時(shí)的程序?qū)嶓w當(dāng)作一種錯(cuò)誤。它的默認(rèn)值是false,也就是說編譯器對(duì)此會(huì)產(chǎn)生一個(gè)警告。

    當(dāng)我們嘗試編譯上面這段程序的時(shí)候,我們將會(huì)得到一個(gè)錯(cuò)誤:

    AnyClass.Old()' is obsolete: 'Don't use Old method, use New method'

    開發(fā)定制特性(custom attributes)

    現(xiàn)在讓我們來看看如何開發(fā)我們自己的特性。

    首先我們要從System.Attribute派生出我們自己的特性類(一個(gè)從System.Attribute抽象類繼承而來的類,不管是直接還是間接繼承,都會(huì)成為一個(gè)特性類。特性類的聲明定義了一種可以被放置在聲明之上新的特性)。

    using System;

    public class HelpAttribute : Attribute

    {

    }

    不管你是否相信,我們已經(jīng)建立了一個(gè)定制特性,現(xiàn)在我們可以用它來裝飾現(xiàn)有的類就好像上面我們使用Obsolete attribute一樣。

    [Help()]

    public class AnyClass

    {

    }

    注意:對(duì)一個(gè)特性類名使用Attribute后綴是一個(gè)慣例。然而,當(dāng)我們把特性添加到一個(gè)程序?qū)嶓w,是否包括Attribute后綴是我們的自由。編譯器會(huì)首先在System.Attribute的派生類中查找被添加的特性類。如果沒有找到,那么編譯器會(huì)添加Attribute后綴繼續(xù)查找。

    到目前為止,這個(gè)特性還沒有起到什么作用。下面我們來添加些東西給它使它更有用些。

    using System;

    public class HelpAttribute : Attribute

    {

    public HelpAttribute(String Descrition_in)

    {

    this.description = Description_in;

    }

    protected String description;

    public String Description

    {

    get

    {

    return this.description;

    }

    }

    }

    [Help("this is a do-nothing class")]

    public class AnyClass

    {

    }

    在上面的例子中,我們給HelpAttribute特性類添加了一個(gè)屬性并且在后續(xù)的部分中我們會(huì)在運(yùn)行時(shí)環(huán)境中查尋它。

    定義或控制特性的使用

    AttributeUsage類是另外一個(gè)預(yù)定義特性類,它幫助我們控制我們自己的定制特性的使用。它描述了一個(gè)定制特性如和被使用。

    AttributeUsage有三個(gè)屬性,我們可以把它放置在定制屬性前面。第一個(gè)屬性是:

    ValidOn

    通過這個(gè)屬性,我們能夠定義定制特性應(yīng)該在何種程序?qū)嶓w前放置。一個(gè)屬性可以被放置的所有程序?qū)嶓w在AttributeTargets enumerator中列出。通過OR操作我們可以把若干個(gè)AttributeTargets值組合起來。

    AllowMultiple

    這個(gè)屬性標(biāo)記了我們的定制特性能否被重復(fù)放置在同一個(gè)程序?qū)嶓w前多次。

    Inherited

    我們可以使用這個(gè)屬性來控制定制特性的繼承規(guī)則。它標(biāo)記了我們的特性能否被繼承。

    下面讓我們來做一些實(shí)際的東西。我們將會(huì)在剛才的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的前面。這也就意味著下面的代碼將會(huì)產(chǎn)生錯(cuò)誤:

    [Help("this is a do-nothing class")]

    public class AnyClass

    {

    [Help("this is a do-nothing method")] //error

    public void AnyMethod()

    {

    }

    }

    編譯器報(bào)告錯(cuò)誤如下:

    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)生了一個(gè)編譯期錯(cuò)誤。

    AnyClass.cs: Duplicate 'Help' attribute

    Ok,現(xiàn)在我們來討論一下最后的這個(gè)屬性。Inherited, 表明當(dāng)特性被放置在一個(gè)基類上時(shí),它能否被派生類所繼承。

    [Help("BaseClass")]

    public class Base

    {

    }

    public class Derive : Base

    {

    }

    這里會(huì)有四種可能的組合:

    [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 ]

    第一種情況:

    如果我們查詢(Query)(稍后我們會(huì)看到如何在運(yùn)行期查詢一個(gè)類的特性)Derive類,我們將會(huì)發(fā)現(xiàn)Help特性并不存在,因?yàn)閕nherited屬性被設(shè)置為false。

    第二種情況:

    和第一種情況相同,因?yàn)閕nherited也被設(shè)置為false。

    第三種情況:

    為了解釋第三種和第四種情況,我們先來給派生類添加點(diǎn)代碼:

    [Help("BaseClass")]

    public class Base

    {

    }

    [Help("DeriveClass")]

    public class Derive : Base

    {

    }

    現(xiàn)在我們來查詢一下Help特性,我們只能得到派生類的屬性,因?yàn)閕nherited被設(shè)置為true,但是AllowMultiple卻被設(shè)置為false。因此基類的Help特性被派生類Help特性覆蓋了。

    第四種情況:

    在這里,我們將會(huì)發(fā)現(xiàn)派生類既有基類的Help特性,也有自己的Help特性,因?yàn)锳llowMultiple被設(shè)置為true。

    定義或控制特性的使用

    AttributeUsage類是另外一個(gè)預(yù)定義特性類,它幫助我們控制我們自己的定制特性的使用。它描述了一個(gè)定制特性如和被使用。

    AttributeUsage有三個(gè)屬性,我們可以把它放置在定制屬性前面。第一個(gè)屬性是:

    ValidOn

    通過這個(gè)屬性,我們能夠定義定制特性應(yīng)該在何種程序?qū)嶓w前放置。一個(gè)屬性可以被放置的所有程序?qū)嶓w在AttributeTargets enumerator中列出。通過OR操作我們可以把若干個(gè)AttributeTargets值組合起來。

    AllowMultiple

    這個(gè)屬性標(biāo)記了我們的定制特性能否被重復(fù)放置在同一個(gè)程序?qū)嶓w前多次。

    Inherited

    我們可以使用這個(gè)屬性來控制定制特性的繼承規(guī)則。它標(biāo)記了我們的特性能否被繼承。

    下面讓我們來做一些實(shí)際的東西。我們將會(huì)在剛才的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的前面。這也就意味著下面的代碼將會(huì)產(chǎn)生錯(cuò)誤:

    [Help("this is a do-nothing class")]

    public class AnyClass

    {

    [Help("this is a do-nothing method")] //error

    public void AnyMethod()

    {

    }

    }

    編譯器報(bào)告錯(cuò)誤如下:

    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, 下面考慮一下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)生了一個(gè)編譯期錯(cuò)誤。

    AnyClass.cs: Duplicate Help attribute

    Ok,現(xiàn)在我們來討論一下最后的這個(gè)屬性。Inherited, 表明當(dāng)特性被放置在一個(gè)基類上時(shí),它能否被派生類所繼承。

    [Help("BaseClass")]

    public class Base

    {

    }

    public class Derive : Base

    {

    }

    這里會(huì)有四種可能的組合:

    [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 ]

    第一種情況:

    如果我們查詢(Query)(稍后我們會(huì)看到如何在運(yùn)行期查詢一個(gè)類的特性)Derive類,我們將會(huì)發(fā)現(xiàn)Help特性并不存在,因?yàn)閕nherited屬性被設(shè)置為false。

    第二種情況:

    和第一種情況相同,因?yàn)閕nherited也被設(shè)置為false。

    第三種情況:

    為了解釋第三種和第四種情況,我們先來給派生類添加點(diǎn)代碼:

    [Help("BaseClass")]

    public class Base

    {

    }

    [Help("DeriveClass")]

    public class Derive : Base

    {

    }

    現(xiàn)在我們來查詢一下Help特性,我們只能得到派生類的屬性,因?yàn)閕nherited被設(shè)置為true,但是AllowMultiple卻被設(shè)置為false。因此基類的Help特性被派生類Help特性覆蓋了。

    第四種情況:

    在這里,我們將會(huì)發(fā)現(xiàn)派生類既有基類的Help特性,也有自己的Help特性,因?yàn)锳llowMultiple被設(shè)置為true。

    ClassMembers = Class | Struct | Enum | Constructor | Method | Property | Field | Event | Delegate | Interface )

    更多信息請(qǐng)查看IT技術(shù)專欄

    更多信息請(qǐng)查看技術(shù)文章
    易賢網(wǎng)手機(jī)網(wǎng)站地址:C# 特性(Attribute)學(xué)習(xí)
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國考·省考課程試聽報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)