特性列表 (Visual Basic)Attribute List (Visual Basic)

指定要应用于已声明的编程元素的特性。Specifies the attributes to be applied to a declared programming element. 用逗号分隔多个属性。Multiple attributes are separated by commas. 下面是一个属性的语法。Following is the syntax for one attribute.

语法Syntax

[ attributemodifier ] attributename [ ( attributearguments | attributeinitializer ) ]  

部件Parts

attributemodifier 应用于源文件开头的属性是必需的。Required for attributes applied at the beginning of a source file. 可以是程序集模块Can be Assembly or Module.
attributename 必需。Required. 属性的名称。Name of the attribute.
attributearguments 可选。Optional. 此特性的位置自变量列表。List of positional arguments for this attribute. 多个参数之间用逗号分隔。Multiple arguments are separated by commas.
attributeinitializer 可选。Optional. 此特性的变量或属性初始值设定项的列表。List of variable or property initializers for this attribute. 多个初始值设定项用逗号分隔。Multiple initializers are separated by commas.

备注Remarks

可以将一个或多个属性应用于几乎所有编程元素(类型、过程、属性等)。You can apply one or more attributes to nearly any programming element (types, procedures, properties, and so forth). 特性显示在程序集的元数据中,它们可帮助你批注代码或指定如何使用特定编程元素。Attributes appear in your assembly's metadata, and they can help you annotate your code or specify how to use a particular programming element. 您可以应用 Visual Basic 和 .NET Framework 定义的属性,并且可以定义自己的属性。You can apply attributes defined by Visual Basic and the .NET Framework, and you can define your own attributes.

有关何时使用属性的详细信息,请参阅属性概述For more information on when to use attributes, see Attributes overview. 有关属性名称的信息,请参阅已声明的元素名称For information on attribute names, see Declared Element Names.

规则Rules

  • 虚拟.Placement. 您可以将特性应用于大多数已声明的编程元素。You can apply attributes to most declared programming elements. 若要应用一个或多个特性,请将特性块置于元素声明的开头。To apply one or more attributes, you place an attribute block at the beginning of the element declaration. "属性" 列表中的每个条目指定要应用的属性,以及用于此属性调用的修饰符和参数。Each entry in the attribute list specifies an attribute you wish to apply, and the modifier and arguments you are using for this invocation of the attribute.

  • 尖括号。Angle Brackets. 如果提供了属性列表,则必须将其括在尖括号("<" 和 ">")中。If you supply an attribute list, you must enclose it in angle brackets ("<" and ">").

  • 声明的一部分。Part of the Declaration. 特性必须是元素声明的一部分,而不是单独的语句。The attribute must be part of the element declaration, not a separate statement. 您可以使用行继续符("_")将声明语句扩展到多个源代码行中。You can use the line-continuation sequence (" _") to extend the declaration statement onto multiple source-code lines.

  • 组成.Modifiers. 在源文件开头应用于编程元素的每个特性都需要特性修饰符(AssemblyModule)。An attribute modifier (Assembly or Module) is required on every attribute applied to a programming element at the beginning of a source file. 应用于不在源文件开头的元素的特性上不允许使用特性修饰符。Attribute modifiers are not allowed on attributes applied to elements that are not at the beginning of a source file.

  • 形参.Arguments. 特性的所有位置参数都必须在任何变量或属性初始值设定项之前。All positional arguments for an attribute must precede any variable or property initializers.

示例Example

下面的示例将 DllImportAttribute 特性应用于 Function 过程的主干定义。The following example applies the DllImportAttribute attribute to a skeleton definition of a Function procedure.

<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 指示特性化过程表示非托管动态链接库(DLL)中的入口点。DllImportAttribute indicates that the attributed procedure represents an entry point in an unmanaged dynamic-link library (DLL). 特性提供 DLL 名称作为位置参数,将其他信息作为变量初始值设定项提供。The attribute supplies the DLL name as a positional argument and the other information as variable initializers.

另请参阅See also