日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#Attributes:定義設(shè)計(jì)期信息

約定:

創(chuàng)新互聯(lián)專注于銅梁網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供銅梁營銷型網(wǎng)站建設(shè),銅梁網(wǎng)站制作、銅梁網(wǎng)頁設(shè)計(jì)、銅梁網(wǎng)站官網(wǎng)定制、微信小程序服務(wù),打造銅梁網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供銅梁網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

     1.”attribute”和”attributes”均不翻譯

     2.”property”譯為“屬性”

     3. msdn中的原句不翻譯

     4.”program entity”譯為”語言元素”

C# Attributes介紹

Attributes是一種新的描述信息,我們既可以使用attributes來定義設(shè)計(jì)期信息(例如 幫助文件,文檔的URL),還可以用attributes定義運(yùn)行時(shí)信息(例如,使XML中的元素與類的成員字段關(guān)聯(lián)起來)。我們也可以用attributes來創(chuàng)建一個(gè)“自描述”的組件。

C# Attributes定義

MSDN 中做如下定義(ms-help://MS.MSDNQTR.2002APR.1033/csspec/html/vclrfcsharpspec_17_2.htm)

"An attribute is a piece of additional declarative information that is specified for a declaration." 

使用預(yù)定義 Attributes

在c#中已有一小組預(yù)定義的attributes,在我們學(xué)習(xí)怎樣創(chuàng)建自定義attributes前,先來了解下在我們的代碼中使用那些預(yù)定義的attributes.

 
 
 
 
  1. using System;  
  2.  
  3. public class AnyClass   
  4.  
  5. {  
  6.     [Obsolete("Don't use Old method, use New method", true)]  
  7.  
  8.     static void Old( ) { }  
  9.  
  10.     static void New( ) { }  
  11.  
  12.     public static void Main( )   
  13.     {  
  14.         Old( );  
  15.     }  
  16. }  
  17.  

仔細(xì)看下該實(shí)例,在該實(shí)例中我們用到了”O(jiān)bsolete”attribute,它標(biāo)記了一個(gè)不該再被使用的語言元素(譯者注:這里的元素為方法),該屬性的***個(gè)參數(shù)是string類型,它解釋為什么該元素被荒棄,以及我們?cè)撌褂檬裁丛貋泶嫠?。?shí)際中,我們可以書寫任何其它文本來代替這段文本。第二個(gè)參數(shù)是告訴編譯器把依然使用這被標(biāo)識(shí)的元素視為一種錯(cuò)誤,這就意味著編譯器會(huì)因此而產(chǎn)生一個(gè)警告。

當(dāng)我們?cè)噲D編譯上面的上面的程序,我們會(huì)得到如下錯(cuò)誤:

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

開發(fā)自定義Attributes

現(xiàn)在我們即將了解怎么開發(fā)自定義的attributes。這兒有個(gè)小小處方,有它我們就可以學(xué)會(huì)創(chuàng)建自定義的attributes。

在C#中,我們的attribute類都派生于System.Attribute類 (A class that derives from the abstract class System.Attribute, whether directly or indirectly, is an attribute class. The declaration of an attribute class defines a new kind of attribute that can be placed on a declaration) ,我們就這么行動(dòng)吧。

 
 
 
 
  1. using System;  
  2.  
  3. public class HelpAttribute : Attribute  
  4.  
  5. {  
  6.  
  7. }  

不管你是否相信我,就這樣我們就已經(jīng)創(chuàng)建了一個(gè)自定義attribute。現(xiàn)在就可以用它來裝飾我們的類了,就像我們使用obsolete attribute一樣。

 
 
 
 
  1. [Help()]  
  2.  
  3. public class AnyClass  
  4.  
  5. {  
  6.  
  7. }  

注意:按慣例我們是用”Attribute“作為attribute類名的后綴,然而,當(dāng)我們當(dāng)我們把a(bǔ)ttribute綁定到某語言元素時(shí),是不包含“Attribute“后綴的。編譯器首先在System.Attribute 的繼承類中查找該attribute,如果沒有找到,編譯器會(huì)把“Attribute“追加到該attribute的名字后面,然后查找它。

但是迄今為止,該attribute沒有任何用處。為了使它有點(diǎn)用處,讓我們?cè)谒锩婕狱c(diǎn)東西吧。

 
 
 
 
  1. using System;  
  2. public class HelpAttribute : Attribute  
  3. {  
  4.     public HelpAttribute(String Descrition_in)  
  5.     {  
  6.         this.description = Description_in;  
  7.     }  
  8.     protected String description;  
  9.     public String Description   
  10.     {  
  11.         get   
  12.         {  
  13.             return this.description;  
  14.                    
  15.         }              
  16.     }      
  17. }  
  18. [Help("this is a do-nothing class")]  
  19. public class AnyClass  
  20. {  
  21. }  
  22.  

在上面的例子中,我們?cè)赼ttribute類中添加了一個(gè)屬性,在***一節(jié)中,我們將在運(yùn)行時(shí)查詢?cè)搶傩浴?/p>

定義或控制自定義Attribute的用法

AttributeUsage 類是另一預(yù)定義類(譯者注:attribute類本身用這個(gè)atrribute System.AttributeUsage來標(biāo)記),它將幫助我們控制我們自定義attribute的用法,這就是,我們能為自定義的attribute類定義attributes。

它描述了一個(gè)自定義attribute類能被怎樣使用。

AttributeUsage 提供三個(gè)屬性,我們能將它們放置到我們的自定義attribute類上, ***個(gè)特性是:

ValidOn

通過這個(gè)屬性,我們能指定我們的自定義attribute可以放置在哪些語言元素之上。這組我們能把自定義attribute類放置其上的語言元素被放在枚舉器AttributeTargets 中。我們可以使用bitwise(譯者注:這個(gè)詞不知道怎么翻譯好,但他的意思是可以這么用:[AttributeUsage((AttributeTargets)4, AllowMultiple = false, Inherited = false )],4代表就是“class”元素,其它諸如1代表“assembly”,16383代表“all”等等)或者”.”操做符綁定幾個(gè)AttributeTargets 值。(譯者注:默認(rèn)值為AttributeTargets.All)

AllowMultiple

該屬性標(biāo)識(shí)我們的自定義attribte能在同一語言元素上使用多次。(譯者注:該屬性為bool類型,默認(rèn)值為false,意思就是該自定義attribute在同一語言元素上只能使用一次)

Inherited

我們可以使用該屬性來控制我們的自定義attribute類的繼承規(guī)則。該屬性標(biāo)識(shí)我們的自定義attribute是否可以由派生類繼承。((譯者注:該屬性為bool類型,默認(rèn)值為false,意思是不能繼承)

讓我們來做點(diǎn)實(shí)際的東西吧,我們將把AttributeUsage attribute 放置在我們的help attribute 上并在它的幫助下,我們來控制help attribute的用法。

 
 
 
 
  1. using System;  
  2.  
  3. [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false )]  
  4.  
  5. public class HelpAttribute : Attribute  
  6.  
  7. {  
  8.  
  9.     public HelpAttribute(String Description_in)  
  10.  
  11.     {  
  12.  
  13.         this.description = Description_in;  
  14.  
  15.     }  
  16.  
  17.     protected String description;  
  18.  
  19.     public String Description  
  20.  
  21.     {  
  22.  
  23.         get   
  24.  
  25.         {  
  26.  
  27.             return this.description;  
  28.  
  29.         }              
  30.  
  31.     }      
  32.  
  33. }  
  34.  

首先我們注意 AttributeTargets.Class. 它規(guī)定這個(gè)help attribute 只能放置在語言元素”class”之上。這就意味著,下面的代碼將會(huì)產(chǎn)生一個(gè)錯(cuò)誤。

AnyClass.cs: Attribute 'Help' is not valid on this declaration type.

It is valid on 'class' declarations only.

現(xiàn)在試著把它綁定到方法。

 
 
 
 
  1. [Help("this is a do-nothing class")]  
  2.  
  3. public class AnyClass  
  4.  
  5. {  
  6.  
  7.     [Help("this is a do-nothing method")]    //error  
  8.  
  9.     public void AnyMethod()  
  10.  
  11.     {  
  12.  
  13.     }  
  14.  
  15. }   
  16.  

我們可以使用 AttributeTargets.All 來允許 Help attribute 可以放置在任何預(yù)定義的語言元素上,那些可能的語言元素如下: 

 
 
 
 
  1. [Help("this is a do-nothing class")]  
  2.  
  3. [Help("it contains a do-nothing method")]  
  4.  
  5. public class AnyClass  
  6.  
  7. {  
  8.  
  9.     [Help("this is a do-nothing method")]        //error  
  10.  
  11.     public void AnyMethod()  
  12.  
  13.     {  
  14.  
  15.     }  
  16.  
  17. }  

它產(chǎn)生了一個(gè)編譯錯(cuò)誤:

AnyClass.cs: Duplicate 'Help' attribute

Ok!現(xiàn)在我們?cè)撚懻撓?**那個(gè)屬性了,”Inherited”, 指出當(dāng)把該attribute放置于一個(gè)基類之上,是否派生類也繼承了該attribute。如果綁定至某個(gè)attribute類的”Inherited”被設(shè)為true,那么該attribute就會(huì)被繼承,然而如果綁定至某個(gè)attribute類的”Inherited”被設(shè)為false或者沒有定義,那么該attribute就不會(huì)被繼承。

讓我們假設(shè)有如下的類關(guān)系。

 
 
 
 
  1. [Help("BaseClass")]   
  2.  
  3. public class Base  
  4.  
  5. {  
  6.  
  7. }  
  8.  
  9.    
  10.  
  11. public class Derive :  Base  
  12.  
  13. {  
  14.  
  15. }  
  16.  

我們有四種可能的綁定:

***種情況

如果我們查詢(我們將在后面來了解如何在運(yùn)行時(shí)來查詢attributes)派生類中的help attribute,我們將不可能查詢到因?yàn)椤盜nherited”被設(shè)為了false。

第二種情況

第二種情況沒有什么不同,因?yàn)槠洹盜nherited”也被設(shè)為了false。

第三種情況

為了解釋第三種和第四種情況,讓我們?yōu)榕缮愐步壎ㄍ籥ttribute。

 
 
 
 
  1. [Help("BaseClass")]   
  2.  
  3. public class Base  
  4.  
  5. {  
  6.  
  7. }  
  8.  
  9. [Help("DeriveClass")]   
  10.  
  11. public class Derive :  Base  
  12.  
  13. {  
  14.  
  15. }  
  16.  

現(xiàn)在我們查詢相關(guān)的help attribute ,我們將僅僅可以得到派生類的attribute,為什么這樣是因?yàn)閔elp attribute雖然允許被繼承,但不能多次在同一語言元素上使用,所以基類中的help attribute被派生類的help attribute 重寫了。

第四種情況

在第四種情況中,當(dāng)我們查詢派生類的help attribute 時(shí),我們可以得到兩個(gè)attributes,當(dāng)然是因?yàn)閔elp attribute既允許被繼承,又允許在同一語言元素上多次使用的結(jié)果。

注意:AttributeUsage attribute 僅應(yīng)用在那種是System.Attribute 派生的attriubte類而且綁定值該attriubte類的AllowMultiple和Inherited均為false上才是有效的。

可選參數(shù) vs. 命名參數(shù)

可選參數(shù)是attribute類構(gòu)造函數(shù)的參數(shù)。它們是強(qiáng)制的,必須在每次在attribute綁定至某語言元素時(shí)提供一個(gè)值。而另一方面,命名參數(shù)倒是真正的可選參數(shù),不是在attribute構(gòu)造函數(shù)的參數(shù)。

為了更加詳細(xì)的解釋,讓我們?cè)贖elp類中添加另外的屬性。

 
 
 
 
  1. [AttributeUsage(AttributeTargets.Class, AllowMultiple = false,  
  2.  
  3.  Inherited = false)]  
  4.  
  5. public class HelpAttribute : Attribute  
  6.  
  7. {  
  8.  
  9.     public HelpAttribute(String Description_in)  
  10.  
  11.     {  
  12.  
  13.         this.description = Description_in;  
  14.  
  15.         this.verion = "No Version is defined for this class";  
  16.  
  17.     }  
  18.  
  19.     protected String description;  
  20.  
  21.     public String Description  
  22.  
  23.     {  
  24.  
  25.         get   
  26.  
  27.         {  
  28.  
  29.             return this.description;  
  30.  
  31.         }  
  32.  
  33.     }  
  34.  
  35.     protected String version;  
  36.  
  37.     public String Version  
  38.  
  39.     {  
  40.  
  41.         get   
  42.  
  43.         {  
  44.  
  45.             return this.version;  
  46.  
  47.         }  
  48.  
  49.         //if we ever want our attribute user to set this property,   
  50.  
  51.         //we must specify set method for it   
  52.  
  53.         set   
  54.  
  55.         {  
  56.  
  57.             this.verion = value;  
  58.  
  59.         }  
  60.  
  61.     }  
  62.  
  63. }  
  64.  
  65. [Help("This is Class1")]  
  66.  
  67. public class Class1  
  68.  
  69. {  
  70.  
  71. }  
  72.  
  73.  
  74.  
  75. [Help("This is Class2", Version = "1.0")]  
  76.  
  77. public class Class2  
  78.  
  79. {  
  80.  
  81. }  
  82.  
  83.  
  84.  
  85. [Help("This is Class3", Version = "2.0",   
  86.  
  87.  Description = "This is do-nothing class")]  
  88.  
  89. public class Class3  
  90.  
  91. {  
  92.  
  93. }  
  94.  

當(dāng)我們?cè)贑lass1中查詢Help attribute已經(jīng)它的屬性,我們將得到:

Help.Description : This is Class1

Help.Version :No Version is defined for this class

因?yàn)槲覀儧]有為Version這個(gè)屬性定義任何任何值,所以在構(gòu)造函數(shù)中設(shè)定的值被我們查詢出來了。如果沒有定義任何值,那么就會(huì)賦一個(gè)該類型的默認(rèn)值(例如:如果是int型,默認(rèn)值就是0)。

現(xiàn)在,查詢Class2的結(jié)果是:

Help.Description : This is Class2

Help.Version :  1.0

我們不能為了可選參數(shù)而使用多個(gè)構(gòu)造函數(shù),應(yīng)該用已命名參數(shù)來代替。我們之所以稱它們?yōu)橐衙?,是因?yàn)楫?dāng)我們?cè)跇?gòu)造函數(shù)為它們提供值時(shí),我們必須命名它們。例如,在第二個(gè)類中,我們?nèi)缡嵌xHelp。

[Help("This is Class2", Version = "1.0")]

在 AttributeUsage 例子中, 參數(shù)”ValidOn”是可選參數(shù),而“Inherited“和“AllowMultiple“ 是命名參數(shù)。

注意:為了在attribute的構(gòu)造函數(shù)中設(shè)定命名參數(shù)的值,我們必須為相應(yīng)的屬性提供一個(gè)set方法否則會(huì)引起編譯期錯(cuò)誤:

'Version' : Named attribute argument can't be a read only property

現(xiàn)在,我們?cè)贑lass3中查找Help attribute 及其屬性會(huì)發(fā)生什么呢?結(jié)果是跟上面提到的相同的編譯期錯(cuò)誤。

'Desciption' : Named attribute argument can't be a read only property

現(xiàn)在我們修改下Help類,為屬性”Description”加一個(gè)set方法?,F(xiàn)在的輸出就是:

Help.Description : This is do-nothing class

Help.Version : 2.0

在屏幕后面究竟發(fā)生了什么呢?首先帶有可選參數(shù)的構(gòu)造函數(shù)被調(diào)用,然后,每個(gè)命名參數(shù)的set方法被調(diào)用,在構(gòu)造函數(shù)中賦給命名參數(shù)的值被set方法所覆寫。

參數(shù)類型

一個(gè)attribute類的參數(shù)類型被限定在如下類型中:

  

Attributes 標(biāo)記

假設(shè),我們想把Help attribute 綁定至元素 assembly。***個(gè)問題是我們要把Help attribute 放在哪兒才能讓編譯器確定該attribute是綁定至整個(gè)assembly呢?考慮另一種情況,我們想把a(bǔ)ttribute綁定至一個(gè)方法的返回類型上,怎樣才能讓編譯器確定我們是把a(bǔ)ttribute綁定至方法的返回類型上,而不是整個(gè)方法呢?

為了解決諸如此類的含糊問題,我們使用attribute標(biāo)識(shí)符,有了它的幫助,我們就可以確切地申明我們把a(bǔ)ttribute 綁定至哪一個(gè)語言元素。

例如:

[assembly: Help("this a do-nothing assembly")]

這個(gè)在Help attribute 前的assembly標(biāo)識(shí)符確切地告訴編譯器,該attribute被綁定至整個(gè)assembly。可能的標(biāo)識(shí)符有: 

在運(yùn)行時(shí)查詢Attributes

現(xiàn)在我們明白怎么創(chuàng)建attribtes和把它們綁定至語言元素。是時(shí)候來學(xué)習(xí)類的使用者該如何在運(yùn)行時(shí)查詢這信息。

為了查詢一語言元素上綁定的attributes,我們必須使用反射。反射有能力在運(yùn)行時(shí)發(fā)現(xiàn)類型信息。

我們可以使用.NET Framework Reflection APIs 通過對(duì)整個(gè)assembly元數(shù)據(jù)的迭代,列舉出assembly中所有已定義的類,類型,還有方法。

記住那舊的Help attribute 和AnyClass 類。

 
 
 
 
  1. using System;  
  2.  
  3. using System.Reflection;  
  4.  
  5. using System.Diagnostics;  
  6.  
  7.  
  8.  
  9. //attaching Help attribute to entire assembly  
  10.  
  11. [assembly : Help("This Assembly demonstrates custom attributes   
  12.  
  13.  creation and their run-time query.")]  
  14.  
  15.  
  16.  
  17. //our custom attribute class  
  18.  
  19. public class HelpAttribute : Attribute  
  20.  
  21. {  
  22.  
  23.     public HelpAttribute(String Description_in)  
  24.  
  25.     {  
  26.  
  27.         //  
  28.  
  29.         // TODO: Add constructor logic here  
  30.  
  31.         this.description = Description_in;  
  32.  
  33.         //  
  34.  
  35.     }  
  36.  
  37.     protected String description;  
  38.  
  39.     public String Description  
  40.  
  41.     {  
  42.  
  43.         get   
  44.  
  45.         {  
  46.  
  47.             return this.deescription;  
  48.  
  49.                    
  50.  
  51.         }              
  52.  
  53.     }      
  54.  
  55. }  
  56.  
  57. //attaching Help attribute to our AnyClass  
  58.  
  59. [HelpString("This is a do-nothing Class.")]  
  60.  
  61. public class AnyClass  
  62.  
  63. {  
  64.  
  65. //attaching Help attribute to our AnyMethod  
  66.  
  67.     [Help("This is a do-nothing Method.")]  
  68.  
  69.     public void AnyMethod()  
  70.  
  71.     {  
  72.  
  73.     }  
  74.  
  75. //attaching Help attribute to our AnyInt Field  
  76.  
  77.     [Help("This is any Integer.")]  
  78.  
  79.     public int AnyInt;  
  80.  
  81. }  
  82.  
  83. class QueryApp  
  84.  
  85. {  
  86.  
  87.     public static void Main()  
  88.  
  89.     {  
  90.  
  91.     }  
  92.  
  93. }  
  94.  

我們將在接下來的兩節(jié)中在我們的Main方法中加入attribute查詢代碼。

查詢程序集的Attributes

在接下來的代碼中,我們先得到當(dāng)前的進(jìn)程名稱,然后用Assembly類中的LoadForm()方法加載程序集,再有用GetCustomAttributes()方法得到被綁定至當(dāng)前程序集的自定義attributes,接下來用foreach語句遍歷所有attributes并試圖把每個(gè)attribute轉(zhuǎn)型為Help attribute(即將轉(zhuǎn)型的對(duì)象使用as關(guān)鍵字有一個(gè)優(yōu)點(diǎn),就是當(dāng)轉(zhuǎn)型不合法時(shí),我們將不需擔(dān)心會(huì)拋出異常,代之以空值(null)作為結(jié)果),接下來的一行就是檢查轉(zhuǎn)型是否有效,及是不是為空,跟著就顯示Help attribute的“Description”屬性。

 
 
 
 
  1. class QueryApp  
  2.  
  3. {  
  4.  
  5.     public static void Main()  
  6.  
  7.     {  
  8.  
  9.         HelpAttribute HelpAttr;  
  10.  
  11.  
  12.         //Querying Assembly Attributes  
  13.  
  14.         String assemblyName;  
  15.  
  16.         Process p = Process.GetCurrentProcess();  
  17.  
  18.         assemblyName = p.ProcessName + ".exe";  
  19.  
  20.  
  21.         Assembly a = Assembly.LoadFrom(assemblyName);  
  22.  
  23.  
  24.         foreach (Attribute attr in a.GetCustomAttributes(true))  
  25.         {  
  26.  
  27.             HelpAttr = attr as HelpAttribute;  
  28.  
  29.             if (null != HelpAttr)  
  30.  
  31.             {  
  32.  
  33.                 Console.WriteLine("Description of {0}:\n{1}",   
  34.  
  35.                                   assemblyName,HelpAttr.Description);  
  36.  
  37.             }  
  38.  
  39.         }  
  40.  
  41. }  
  42.  
  43. }  
  44.  

程序輸出如下:

Description of QueryAttribute.exe:

This Assembly demonstrates custom attributes creation and

their run-time query.

Press any key to continue

查詢類、方法、類成員的Attributes

下面的代碼中,我們惟一不熟悉的就是Main()方法中的***行。

Type type = typeof(AnyClass);

它用typeof操作符得到了一個(gè)與我們AnyClass類相關(guān)聯(lián)的Type型對(duì)象。剩下的查詢類attributes代碼就與上面的例子是相似的,應(yīng)該不要解釋了吧(我是這么想的)。

為查詢方法和類成員的attributes,首先我們得到所有在類中存在的方法和成員,然后我們查詢與它們相關(guān)的所有attributes,這就跟我們查詢類attributes一樣的方式。

 
 
 
 
  1. class QueryApp  
  2.  
  3. {  
  4.  
  5.     public static void Main()  
  6.  
  7.     {  
  8.  
  9.  
  10.  
  11.         Type type = typeof(AnyClass);  
  12.  
  13.         HelpAttribute HelpAttr;  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.         //Querying Class Attributes  
  20.  
  21.         foreach (Attribute attr in type.GetCustomAttributes(true))  
  22.  
  23.         {  
  24.  
  25.             HelpAttr = attr as HelpAttribute;  
  26.  
  27.             if (null != HelpAttr)  
  28.  
  29.             {  
  30.  
  31.                 Console.WriteLine("Description of AnyClass:\n{0}",   
  32.  
  33.                                   HelpAttr.Description);  
  34.  
  35.             }  
  36.  
  37.         }  
  38.  
  39.         //Querying Class-Method Attributes    
  40.  
  41.         foreach(MethodInfo method in type.GetMethods())  
  42.  
  43.         {  
  44.  
  45.             foreach (Attribute attr in method.GetCustomAttributes(true))  
  46.  
  47.             {  
  48.  
  49.                 HelpAttr = attr as HelpAttribute;  
  50.  
  51.                 if (null != HelpAttr)  
  52.  
  53.                 {  
  54.  
  55.                     Console.WriteLine("Description of {0}:\n{1}",   
  56.  
  57.                                       method.Name,   
  58.  
  59.                                       HelpAttr.Description);  
  60.  
  61.                 }  
  62.  
  63.             }  
  64.  
  65.         }  
  66.  
  67.         //Querying Class-Field (only public) Attributes  
  68.  
  69.         foreach(FieldInfo field in type.GetFields())  
  70.  
  71.         {  
  72.  
  73.             foreach (Attribute attr in field.GetCustomAttributes(true))  
  74.  
  75.             {  
  76.  
  77.                 HelpAttr= attr as HelpAttribute;  
  78.  
  79.                 if (null != HelpAttr)  
  80.  
  81.                 {  
  82.  
  83.                     Console.WriteLine("Description of {0}:\n{1}",  
  84.  
  85.                                       field.Name,HelpAttr.Description);  
  86.  
  87.                 }  
  88.  
  89.             }  
  90.  
  91.         }  
  92.  
  93.     }  
  94.  
  95. }  
  96.  

以上就介紹了C# Attributes的一些使用方法。

【編輯推薦】

  1. 淺談C#中構(gòu)造函數(shù)和成員函數(shù)
  2. C#函數(shù)的參數(shù)返回結(jié)構(gòu)數(shù)組
  3. 概述ASP.NET中的NGWS Runtime
  4. C#函數(shù)與JavaScript函數(shù)
  5. 詳解C# Object.Equals函數(shù)

本文名稱:C#Attributes:定義設(shè)計(jì)期信息
地址分享:http://www.5511xx.com/article/ccedpgj.html