屬性清單 (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. 您可以將屬性(attribute)套用至大部分宣告的程式設計項目。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 屬性套用至 @no__t 1 程式的基本架構定義。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