使用屬性擴充中繼資料Extending Metadata Using Attributes

Common Language Runtime 可讓您加入稱為屬性 (Attribute) 之類似關鍵字的描述性宣告,以標註類型、欄位、方法和屬性 (Property) 等程式設計項目。The common language runtime allows you to add keyword-like descriptive declarations, called attributes, to annotate programming elements such as types, fields, methods, and properties. 當您編譯執行階段的程式碼時,它會轉換成 Microsoft 中繼語言 (MSIL),並與編譯器所產生的中繼資料一起放在可攜式執行檔 (PE) 中。When you compile your code for the runtime, it is converted into Microsoft intermediate language (MSIL) and placed inside a portable executable (PE) file along with metadata generated by the compiler. 屬性可讓您將額外的描述性資訊放入中繼資料,其可使用執行階段反映服務來擷取。Attributes allow you to place extra descriptive information into metadata that can be extracted using runtime reflection services. 編譯器會在您宣告衍生自 System.Attribute 的特殊類別執行個體時建立屬性。The compiler creates attributes when you declare instances of special classes that derive from System.Attribute.

.NET Framework 會針對各種原因使用屬性來解決一些問題。The .NET Framework uses attributes for a variety of reasons and to address a number of issues. 屬性描述如何序列化資料、指定用來強制執行安全性的特性,以及限制 Just-in-Time (JIT) 編譯器的最佳化程度,讓程式碼保持易於偵錯。Attributes describe how to serialize data, specify characteristics that are used to enforce security, and limit optimizations by the just-in-time (JIT) compiler so the code remains easy to debug. 屬性也可記錄檔案名稱或程式碼作者,或者在表單開發期間控制控制項和成員的可見性。Attributes can also record the name of a file or the author of code, or control the visibility of controls and members during forms development.

標題Title 說明Description
套用屬性Applying Attributes 描述如何將屬性套用至您程式碼的項目。Describes how to apply an attribute to an element of your code.
撰寫自訂屬性Writing Custom Attributes 描述如何設計自訂屬性類別。Describes how to design custom attribute classes.
擷取儲存於屬性中的資訊Retrieving Information Stored in Attributes 描述如何針對載入執行內容中的程式碼擷取自訂屬性。Describes how to retrieve custom attributes for code that is loaded into the execution context.
中繼資料和自我描述元件Metadata and Self-Describing Components 提供中繼資料的概觀,並描述如何在 .NET Framework 可攜式執行檔 (PE) 中實作。Provides an overview of metadata and describes how it is implemented in a .NET Framework portable executable (PE) file.
如何:將組件載入到僅限反映的內容將組件載入到僅限反映的內容How to: Load Assemblies into the Reflection-Only Context 說明如何擷取僅限反映的內容中的自訂屬性資訊。Explains how to retrieve custom attribute information in the reflection-only context.

參考資料Reference

System.Attribute