国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C# 特性(Attribute)入門(二)

2024-07-21 02:19:51
字體:
來源:轉載
供稿:網友
定義或控制特性的使用

attributeusage類是另外一個預定義特性類,它幫助我們控制我們自己的定制特性的使用。它描述了一個定制特性如和被使用。

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



validon

通過這個屬性,我們能夠定義定制特性應該在何種程序實體前放置。一個屬性可以被放置的所有程序實體在attributetargets enumerator中列出。通過or操作我們可以把若干個attributetargets值組合起來。



allowmultiple

這個屬性標記了我們的定制特性能否被重復放置在同一個程序實體前多次。



inherited

我們可以使用這個屬性來控制定制特性的繼承規則。它標記了我們的特性能否被繼承。



下面讓我們來做一些實際的東西。我們將會在剛才的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。它規定了help特性只能被放在class的前面。這也就意味著下面的代碼將會產生錯誤:

[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特性被放置在任何程序實體前。可能的值是:

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。它規定了特性不能被重復放置多次。

[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()
{
}
}
它產生了一個編譯期錯誤。

anyclass.cs: duplicate 'help' attribute



ok,現在我們來討論一下最后的這個屬性。inherited, 表明當特性被放置在一個基類上時,它能否被派生類所繼承。



[help("baseclass")]
public class base
{
}

public class derive : base
{
}
這里會有四種可能的組合:

[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)(稍后我們會看到如何在運行期查詢一個類的特性)derive類,我們將會發現help特性并不存在,因為inherited屬性被設置為false。



第二種情況:

和第一種情況相同,因為inherited也被設置為false。



第三種情況:

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

[help("baseclass")]
public class base
{
}
[help("deriveclass")]
public class derive : base
{
}
現在我們來查詢一下help特性,我們只能得到派生類的屬性,因為inherited被設置為true,但是allowmultiple卻被設置為false。因此基類的help特性被派生類help特性覆蓋了。



第四種情況:

在這里,我們將會發現派生類既有基類的help特性,也有自己的help特性,因為allowmultiple被設置為true。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿坝| 天气| 蒲江县| 纳雍县| 米林县| 永嘉县| 漾濞| 阆中市| 奈曼旗| 新余市| 武平县| 赣州市| 临邑县| 乾安县| 体育| 襄城县| 旺苍县| 长宁区| 铜川市| 东台市| 宜良县| 广宗县| 福建省| 平顶山市| 礼泉县| 永宁县| 阜康市| 南郑县| 长岛县| 祁门县| 灵寿县| 涞水县| 鱼台县| 鄄城县| 阿克苏市| 邵武市| 五常市| 文昌市| 孝昌县| 成都市| 延庆县|