日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C# 特性(Attribute)学习。

發布時間:2023/11/27 生活经验 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 特性(Attribute)学习。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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

  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( );
????? }
}



  我們先來看一下上面這個例子,在這個例子中我們使用了Obsolete特性,它標記了一個不應該再被使用的程序實體。第一個參數是一個字符串,它解釋了為什么該實體是過時的以及應該用什么實體來代替它。實際上,你可以在這里寫任何文本。第二個參數告訴編譯器應該把使用這個過時的程序實體當作一種錯誤。它的默認值是false,也就是說編譯器對此會產生一個警告。

  當我們嘗試編譯上面這段程序的時候,我們將會得到一個錯誤:

  AnyClass.Old()' is obsolete: 'Don't use Old method, use New method'
  開發定制特性(custom attributes)

  現在讓我們來看看如何開發我們自己的特性。

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

using System;
public class HelpAttribute : Attribute
{
}



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

[Help()]
public class AnyClass
{
}



  注意:對一個特性類名使用Attribute后綴是一個慣例。然而,當我們把特性添加到一個程序實體,是否包括Attribute后綴是我們的自由。編譯器會首先在System.Attribute的派生類中查找被添加的特性類。如果沒有找到,那么編譯器會添加Attribute后綴繼續查找。

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

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特性類添加了一個屬性并且在后續的部分中我們會在運行時環境中查尋它。

  定義或控制特性的使用

  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特性被放置在任何程序實體前??赡艿闹凳?#xff1a;

  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。

定義或控制特性的使用

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,? 下面考慮一下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。
ClassMembers?=?Class?|?Struct?|?Enum?|?Constructor?|?Method?|?Property?|?Field?|?Event?|?Delegate?|?Interface?)

總結

以上是生活随笔為你收集整理的C# 特性(Attribute)学习。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。