C#擷取屬性

來源:互聯網
上載者:User

 

注意 如何從System.Attribute類中繼承 以及如何使用建構函式和屬性來定義attribute參數
可以在VB類中使用該attribute 方法如下
<Author("Julian")>
Public Class Fred
...
End Class
在C++中 可以使用attribute的 attribute 來建立一個託管類或者結構
以代表一個自訂attribute 注意這個類不必從System.Attribute中繼承

[attribute(target)]
public __gc class Author {
...
};
如果 attribute 要用於其他 assembly那麼類必須是公用的target
參數是System.AttributeTargets 枚舉的一個參數 如前面表 2.5 所示 在 attribute 類中 建構函式用於指定位置參數 而資料成員和屬性則用於實現具名引數 下面是Author attribute的C++代碼
[attribute(Class)] public __gc class Author { String *authorName; // positional parameter String *lastModDate; // named parameter public: __property String* get_Name () { return authorName; } __property String* get_ModDate () { return lastModDate; } __property String* set_ModDate (String* date) { lastModDate = date; } Author(String* name) { authorName = name; } }; 可以將該attribute加入到一個類中 如下所示 [Author("Julian", Date="21/12/00")] public __gc class Foo { }; 可以看到 因為Data是一個具名引數 它由關鍵字指定 並且位於參數列表的末尾 在C#中 建立attribute的方法是類似的 但有兩點不同 [AttributeUsage(AttributeTargets.Class)] public class Author : System.Attribute { private string authorName; // positional parameter private string lastModDate; // named parameter 第2章 .NET編程模型 71 public string Name { get { return authorName; } } public string ModDate { get { return lastModDate; } set { lastModDate = value; } } Author(string name) { authorName = name; } }; 第一個區別是 attribute類從System.Attribute繼承而來 而C++中則不需要任何繼承第二個區別是 attributeusage attribute用於控制該attribute的使用範圍 除此之外 代碼在結構上是很類似的 使用建構函式實現位置參數 使用屬性實現具名引數

2.2.19 查詢attribute
儘管大多數attribute都由編譯器建立 並由CLR使用 但是有時候程式員還需要知道某些項處理特定attribute的範圍 並且能夠檢查這些attribute 為了找到有關 attribute 需要瞭解 System.Type 類 Type 是表示類 介面 屬性 實值型別以及枚舉等類型定義的一種類 它能夠找出許多關於類型及其屬性的詳細資料 但是 使用者只需要知道如何使用類來擷取這些attribute資訊即可 注意 如果瞭解C++ 可以考慮使用RTTI將會有所協助 下面考察 VB中是如何檢索attribute資訊的執行個體 並對該代碼進行分析 仍然使用前一節的Author attribute執行個體 <Author("Julian", Date="21/12/00")> Public Class Foo ... End Class 獲得一個Type對象以表示Foo類 而後查詢該類 可以知道Foo類是否具有一個Author attribute

在C#中實現該操作幾乎是完全相同的

 

using System; 
...
Type tf = typeof(Foo);
object[] atts = tf.GetCustomAttributes(true);
foreach(object o in atts) {
if(o.GetType().Equals(typeof(Author)))
Console.WriteLine("Foo has an Author attribute");
}

 

 

 

第一件事就是 使用Typeof操作符擷取一個所要查詢的類的Type對象
而後可以使用GetCustomAttributes()方法來擷取一組該對象所支援的自訂attribute的引用列表
因為將返回普通對象引用的數組 因此需要檢查對象的類型 以確認它們中是否有 Author 項
注意GetType()方法的使用 它將從對象引用中返回一個 Type 對象
這與 typeof 操作符相對應後者從類名稱中返回一個 Author 屬性
一旦對該類找到了一個 Author 屬性 就可以檢索該參數

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.