c# 特性 attribute
特性(attribute)是用于在運行時傳遞程序中各種元素(比如類、方法、結構、枚舉、組件等)的行為信息的聲明性標簽。您可以通過使用特性向程序添加聲明性信息。一個聲明性標簽是通過放置在它所應用的元素前面的方括號([ ])來描述的。
特性(attribute)用于添加元數據,如編譯器指令和注釋、描述、方法、類等其他信息。.net 框架提供了兩種類型的特性:預定義特性和自定義特性。
1. 規定特性(attribute)
規定特性(attribute)的語法如下:
[attribute(positional_parameters, name_parameter = value, ...)] element
特性(attribute)的名稱和值是在方括號內規定的,放置在它所應用的元素之前。positional_parameters 規定必需的信息,name_parameter 規定可選的信息。
2. 預定義特性(attribute)
.net 框架提供了三種預定義特性:
- attributeusage
- conditional
- obsolete
1) attributeusage
預定義特性 attributeusage 描述了如何使用一個自定義特性類。它規定了特性可應用到的項目的類型。
規定該特性的語法如下:
[attributeusage( validon, allowmultiple=allowmultiple, inherited=inherited )]
其中:
- 參數 validon 規定特性可被放置的語言元素。它是枚舉器 attributetargets 的值的組合。默認值是 attributetargets.all。
- 參數 allowmultiple(可選的)為該特性的 allowmultiple 屬性(property)提供一個布爾值。如果為 true,則該特性是多用的。默認值是 false(單用的)。
- 參數 inherited(可選的)為該特性的 inherited 屬性(property)提供一個布爾值。如果為 true,則該特性可被派生類繼承。默認值是 false(不被繼承)。
例如:
[attributeusage(attributetargets.class | attributetargets.constructor | attributetargets.field | attributetargets.method | attributetargets.property, allowmultiple = true)]
2) conditional
這個預定義特性標記了一個條件方法,其執行依賴于指定的預處理標識符。
它會引起方法調用的條件編譯,取決于指定的值,比如 debug 或 trace。例如,當調試代碼時顯示變量的值。
規定該特性的語法如下:
[conditional( conditionalsymbol )]
例如:
[conditional("debug")]
下面的范例演示了該特性:
#define debug using system; using system.diagnostics; public class myclass { ? ? [conditional("debug")] ? ? public static void message(string msg) ? ? { ? ? ? ? console.writeline(msg); ? ? } } class test { ? ? static void function1() ? ? { ? ? ? ? myclass.message("in function 1."); ? ? ? ? function2(); ? ? } ? ? static void function2() ? ? { ? ? ? ? myclass.message("in function 2."); ? ? } ? ? public static void main() ? ? { ? ? ? ? myclass.message("in main function."); ? ? ? ? function1(); ? ? ? ? console.readkey(); ? ? } }
當上面的代碼被編譯和執行時,它會產生下列結果:
in main function in function 1 in function 2
3) obsolete
這個預定義特性標記了不應被使用的程序實體。它可以讓您通知編譯器丟棄某個特定的目標元素。例如,當一個新方法被用在一個類中,但是您仍然想要保持類中的舊方法,您可以通過顯示一個應該使用新方法,而不是舊方法的消息,來把它標記為 obsolete(過時的)。
規定該特性的語法如下:
[obsolete( message )] [obsolete( message, iserror )]
其中:
- 參數 message,是一個字符串,描述項目為什么過時以及該替代使用什么。
- 參數 iserror,是一個布爾值。如果該值為 true,編譯器應把該項目的使用當作一個錯誤。默認值是 false(編譯器生成一個警告)。
下面的范例演示了該特性:
using system; public class myclass { ? ?[obsolete("don't use oldmethod, use newmethod instead", true)] ? ?static void oldmethod() ? ?{ ? ? ? console.writeline("it is the old method"); ? ?} ? ?static void newmethod() ? ?{ ? ? ? console.writeline("it is the new method"); ? ?} ? ?public static void main() ? ?{ ? ? ? oldmethod(); ? ?} }
當您嘗試編譯該程序時,編譯器會給出一個錯誤消息說明:
don't use oldmethod, use newmethod instead
3. 創建自定義特性(attribute)
.net 框架允許創建自定義特性,用于存儲聲明性的信息,且可在運行時被檢索。該信息根據設計標準和應用程序需要,可與任何目標元素相關。
創建并使用自定義特性包含四個步驟:
- 聲明自定義特性
- 構建自定義特性
- 在目標程序元素上應用自定義特性
- 通過反射訪問特性
最后一個步驟包含編寫一個簡單的程序來讀取元數據以便查找各種符號。元數據是用于描述其他數據的數據和信息。該程序應使用反射來在運行時訪問特性。我們將在下一章詳細討論這點。
1) 聲明自定義特性
一個新的自定義特性應派生自 system.attribute 類。例如:
// 一個自定義特性 bugfix 被賦給類及其成員 [attributeusage(attributetargets.class | attributetargets.constructor | attributetargets.field | attributetargets.method | attributetargets.property, allowmultiple = true)] public class debuginfo : system.attribute
在上面的代碼中,我們已經聲明了一個名為 debuginfo 的自定義特性。
2) 構建自定義特性
讓我們構建一個名為 debuginfo 的自定義特性,該特性將存儲調試程序獲得的信息。它存儲下面的信息:
- bug 的代碼編號
- 辨認該 bug 的開發人員名字
- 最后一次審查該代碼的日期
- 一個存儲了開發人員標記的字符串消息
我們的 debuginfo 類將帶有三個用于存儲前三個信息的私有屬性(property)和一個用于存儲消息的公有屬性(property)。所以 bug 編號、開發人員名字和審查日期將是 debuginfo 類的必需的定位( positional)參數,消息將是一個可選的命名(named)參數。
每個特性必須至少有一個構造函數。必需的定位( positional)參數應通過構造函數傳遞。下面的代碼演示了 debuginfo 類:
// 一個自定義特性 bugfix 被賦給類及其成員 [attributeusage(attributetargets.class | attributetargets.constructor | attributetargets.field | attributetargets.method | attributetargets.property, allowmultiple = true)] public class debuginfo : system.attribute { ? private int bugno; ? private string developer; ? private string lastreview; ? public string message; ? public debuginfo(int bg, string dev, string d) ? { ? ? ? this.bugno = bg; ? ? ? this.developer = dev; ? ? ? this.lastreview = d; ? } ? public int bugno ? { ? ? ? get ? ? ? { ? ? ? ? ? return bugno; ? ? ? } ? } ? public string developer ? { ? ? ? get ? ? ? { ? ? ? ? ? return developer; ? ? ? } ? } ? public string lastreview ? { ? ? ? get ? ? ? { ? ? ? ? ? return lastreview; ? ? ? } ? } ? public string message ? { ? ? ? get ? ? ? { ? ? ? ? ? return message; ? ? ? } ? ? ? set ? ? ? { ? ? ? ? ? message = value; ? ? ? } ? } }
3) 應用自定義特性
通過把特性放置在緊接著它的目標之前,來應用該特性:
[debuginfo(45, "zara ali", "12/8/2012", message = "return type mismatch")] [debuginfo(49, "nuha ali", "10/10/2012", message = "unused variable")] class rectangle { ? // 成員變量 ? protected double length; ? protected double width; ? public rectangle(double l, double w) ? { ? ? ? length = l; ? ? ? width = w; ? } ? [debuginfo(55, "zara ali", "19/10/2012", ? message = "return type mismatch")] ? public double getarea() ? { ? ? ? return length * width; ? } ? [debuginfo(56, "zara ali", "19/10/2012")] ? public void display() ? { ? ? ? console.writeline("length: {0}", length); ? ? ? console.writeline("width: {0}", width); ? ? ? console.writeline("area: {0}", getarea()); ? } }
在下一章中,我們將使用 reflection 類對象來檢索這些信息。