套用屬性

使用下列程序將屬性套用到您的程式碼項目。

  1. 從 .NET Framework 匯入它的命名空間 (Namespace) 來定義新屬性或使用現存的屬性。

  2. 將屬性 (Attribute) 放在緊接於程式碼項目之前的位置,藉此將屬性套用到此項目。

    每個語言皆有自己的屬性語法。 在 C++ 和 C# 中,屬性是以方括號括住,並以可包含分行符號的泛空白字元 (White Space) 與項目分隔。 在 Visual Basic 中,屬性是以角括弧括住,並且必須位於相同的邏輯程式敘述行 (Logical Line);如果需要使用分行符號,可以使用行接續符號字元。 在 J# 中,會使用特殊的註解語法附加屬性。

  3. 指定屬性的位置參數和具名參數。

    位置參數是必要項,並且必須在任何具名參數之前,且會對應到一個屬性建構函式 (Constructor) 的參數。 具名參數是選擇性參數,且會對應到屬性 (Attribute) 的讀取/寫入屬性 (Property)。 在 C++、C# 和 J# 中,為每個選擇性參數指定 name=value,其中 name 為屬性的名稱。 在 Visual Basic 中,指定 name:=value。

此屬性會在您編譯程式碼時發送至中繼資料內,並且透過執行階段反映服務,在 Common Language Runtime 和任何自訂工具或應用程式中使用。

依照慣例,所有屬性名稱以 Attribute 作結尾。 然而,許多以執行階段為目標的語言,例如 Visual Basic 和 C#,不需要您指定屬性的完整名稱。 例如,如果您想要初始化 System.ObsoleteAttribute,就只需要以 Obsolete 參考它。

將屬性套用至方法

下列程式碼範例示範如何宣告 System.ObsoleteAttribute,其標記程式碼為過時的。 字串 "Will be removed in next version" 會傳遞至屬性。 當呼叫屬性所描述的程式碼時,這個屬性產生編譯器警告以顯示傳遞的字串。

Public Class Example
    ' Specify attributes between square brackets in C#.
    ' This attribute is applied only to the Add method.
    <Obsolete("Will be removed in next version.")>
    Public Shared Function Add(a As Integer, b As Integer) As Integer
        Return a + b
    End Function
End Class

Class Test
    Public Shared Sub Main()
        ' This generates a compile-time warning.
        Dim i As Integer = Example.Add(2, 2)
    End Sub
End Class
public class Example
{
    // Specify attributes between square brackets in C#.
    // This attribute is applied only to the Add method.
    [Obsolete("Will be removed in next version.")]
    public static int Add(int a, int b)
    {
        return (a + b);
    }
}

class Test
{
    public static void Main()
    {
        // This generates a compile-time warning.
        int i = Example.Add(2, 2);
    }
}
public ref class Example
{
    // Specify attributes between square brackets in C#.
    // This attribute is applied only to the Add method.
public:
    [Obsolete("Will be removed in next version.")]
    static int Add(int a, int b)
    {
        return (a + b);
    }
};

ref class Test
{
public:
    static void Main()
    {
        // This generates a compile-time warning.
        int i = Example::Add(2, 2);
    }
};

int main()
{
    Test::Main();
}

在組件層級套用屬性

如果您想要在組件 (Assembly) 層級套用屬性,請使用 Assembly 關鍵字。 下列程式碼示範在組件層級套用 AssemblyNameAttribute

Imports System.Reflection
<Assembly:AssemblyTitle("My Assembly")>
using System.Reflection;
[assembly:AssemblyTitle("My Assembly")]
using namespace System::Reflection;
[assembly:AssemblyTitle("My Assembly")];

當套用這個屬性後,"MyAssembly" 字串將被放在檔案的中繼資料裡的組件資訊清單 (Assembly Manifest) 中。 您可以使用 MSIL 反組譯工具 (Ildasm.exe) 或者建立擷取屬性的自訂程式來檢視屬性。

請參閱

參考

屬性 (C# 和 Visual Basic)

概念

使用屬性擴充中繼資料

擷取儲存於屬性中的資訊

其他資源

Attributed Programming Concepts