人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

C# 特征(Attribute)基礎(二)

C# 特征(Attribute)基礎(二)

更新時間:2022-06-29 文章作者:未知 信息來源:網絡 閱讀次數:

定義或控制特性的使用

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。

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 含山县| 汝州市| 齐齐哈尔市| 罗江县| 石楼县| 深州市| 吉林省| 平阳县| 博野县| 汝阳县| 崇文区| 大兴区| 浮梁县| 内丘县| 云安县| 闸北区| 英山县| 台东县| 高州市| 鹿泉市| 吉首市| 汉沽区| 莱阳市| 八宿县| 萝北县| 安顺市| 大厂| 基隆市| 商都县| 阿拉善盟| 托克托县| 龙江县| 吉木萨尔县| 高青县| 怀宁县| 岢岚县| 开远市| 阳城县| 房山区| 金昌市| 开阳县|