属性列表

更新:2007 年 11 月

指定要应用于已声明的编程元素的属性 (Attribute)。多个属性以逗号分隔。以下是一个属性的语法。

[ attributemodifier ] attributename [ ( attributearguments | attributeinitializer ) ]

各部分说明

  • attributemodifier
    对于用于源文件起始处的属性 (Attribute),为必选项。可以为 AssemblyModule

  • attributename
    必选。属性 (Attribute) 名。

  • attributearguments
    可选。该属性 (Attribute) 的定位参数列表。多个参数以逗号分隔。

  • attributeinitializer
    可选。该属性 (Attribute) 的变量或属性 (Property) 初始值设定项列表。多个初始值设定项以逗号分隔。

备注

可以将一个或多个属性 (Attribute) 应用于几乎任何一个编程元素(类型、过程、属性 (Property) 等)。属性 (Attribute) 显示在程序集的元数据中,它们可以帮助您批注代码或指定如何使用特定的编程元素。您可以应用 Visual Basic 和 .NET Framework 定义的属性,也可以定义您自己的属性 (Attribute)。

有关何时使用属性 (Attribute) 的更多信息,请参见 Visual Basic 中的属性 (Attribute)特性的常见用途。有关属性 (Attribute) 名称的信息,请参见已声明元素的名称

规则

  • **位置。**可以将属性 (Attribute) 应用于大多数已声明的编程元素。若要应用一个或多个属性 (Attribute),请在元素声明的起始处放置“属性块”。属性 (Attribute) 列表中的每一项均指定一个您要应用的属性 (Attribute) 以及用于该属性 (Attribute) 调用的修饰符和参数。

  • **尖括号。**如果您提供了属性列表,必须将其置于尖括号内(“<”和“>”)。

  • **声明的一部分。**属性 (Attribute) 必须为元素声明的一部分,而不是独立的语句。您可以使用行继续序列 (" _") 将声明语句扩展到多个源代码行。

  • **修饰符。**在应用于源文件起始处的编程元素的每个属性 (Attribute) 中,必须使用属性修饰符(Assembly 或 Module)。在非应用于源文件起始处的元素的属性 (Attribute) 中,不允许使用属性修饰符。

  • **参数。**属性 (Attribute) 的所有定位参数必须位于任何变量或属性 (Property) 初始值设定项之前。

示例

下面的示例将 DllImportAttribute 属性 (Attribute) 应用于 Function 过程的主干定义中。

<DllImportAttribute("kernel32.dll", EntryPoint:="MoveFileW", _
    SetLastError:=True, CharSet:=CharSet.Unicode, _
    ExactSpelling:=True, _
    CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function moveFile(ByVal src As String, _
  ByVal dst As String) As Boolean
    ' This function copies a file from the path src to the path dst.
    ' Leave this function empty. The DLLImport attribute forces calls
    ' to moveFile to be forwarded to MoveFileW in KERNEL32.DLL.
End Function

DllImportAttribute 指示该属性 (Attribute) 过程表示非托管动态链接库 (DLL) 中的一个入口点。该属性 (Attribute) 提供 DLL 名称作为一个定位参数,并提供其他信息作为变量初始值设定项。

请参见

任务

如何:在代码中拆分和合并语句

概念

特性的常见用途

属性的应用

参考

Assembly

Module (Visual Basic)

其他资源

Visual Basic 中的属性 (Attribute)