应用特性
通过下列过程将特性应用到代码元素。
定义新特性,或者通过从 .NET Framework 导入特性的命名空间,使用现有特性。
在紧邻代码元素之前放置特性,从而将该特性应用于代码元素。
每种语言都有自己的特性语法。 在 C++ 和 C# 中,特性由方括号括起来,并且通过空白(可包括换行符)与元素分隔。 在 Visual Basic 中,特性由尖括号括起来,并且必须处于同一逻辑行;如果需要加换行符,则可以使用行继续字符。 在 J# 中,使用特殊注释语法附加特性。
为特性指定位置参数和命名参数。
位置参数是必需的,并且必须放在所有命名参数之前;位置参数对应于特性的构造函数之一的参数。 命名参数是可选的,对应于特性的读/写属性。 在 C++、C# 和 J# 中,为每个可选参数指定 name=value,其中 name 是属性的名称。 在 Visual Basic 中,指定 name:=value。
特性在您编译代码时被发送到元数据中,并可通过运行时反射服务用于公共语言运行时以及任何自定义工具或应用程序。
按照约定,所有特性名都以 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 关键字。 下列代码显示在程序集级别应用的 AssemblyNameAttribute。
Imports System.Reflection
<Assembly:AssemblyTitle("My Assembly")>
using System.Reflection;
[assembly:AssemblyTitle("My Assembly")]
using namespace System::Reflection;
[assembly:AssemblyTitle("My Assembly")];
应用该特性时,字符串 "MyAssembly" 被放到文件元数据部分的程序集清单中。 可以使用 MSIL 反汇编程序 (Ildasm.exe) 或通过创建检索该特性的自定义程序来查看该特性。