应用属性

使用以下过程将特性应用于代码的元素。

  1. 定义新特性或使用现有的 .NET 特性。

  2. 通过将特性置于紧邻元素之前,将该特性应用于代码元素。

    每种语言都有其自己的特性语法。 在 C++ 和 C# 中,特性是用方括号括起来的,并通过空格与元素分开(可能包括换行符)。 在 Visual Basic 中,特性是用尖括号括起来的,且必须位于同一逻辑线上;如果需要换行符,可使用行继续字符。

  3. 指定特性的位置参数和命名参数。

    位置参数是必需的,且必须位于所有命名参数之前;它们对应于特性的其中一个构造函数的参数。 命名参数是可选的且对应于特性的读/写属性。 在 C++ 和 C# 中,为每个可选参数指定 name=value,其中 name 是属性名。 在 Visual Basic 中,指定 name:=value

编译代码时,特性将被发到元数据中,并且通过运行时反射服务可用于公共语言运行时和任何自定义工具或应用程序。

按照惯例,所有特性名称都以“Attribute”结尾。 但是,面向运行时的几种语言(如 Visual Basic 和 C#)无需指定特性的全名。 例如,若要初始化 System.ObsoleteAttribute,只需将它引用为 Obsolete 即可。

将特性应用于方法

以下代码示例显示如何使用 System.ObsoleteAttribute(其将代码标记为已过时)。 将字符串 "Will be removed in next version" 传递给特性。 当特性描述的代码被调用时,此特性会导致产生编译器警告,显示传递的字符串。

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();
}
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 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

在程序集级别应用特性

如果要在程序集级别应用属性,请使用 assemblyAssembly(Visual Basic 中用assembly )关键字。 下列代码显示在程序集级别应用的 AssemblyTitleAttribute

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

应用此特性时,字符串 "My Assembly" 将被放置在文件元数据部分的程序集清单中。 可通过后列方法查看特性:使用 MSIL 反汇编程序 (Ildasm.exe),或创建一个自定义程序来检索特性。

请参阅