屬性的應用

更新:2007 年 11 月

藉由將屬性 (Attribute) 區塊加入如屬性 (Property)、方法、事件、類別和組件等程式項目,就可以套用屬性 (Attribute)。屬性 (Attribute) 區塊是由角括弧 (< >) 內含逗號分隔的屬性 (Attribute) 宣告清單所組成的。屬性 (Attribute) 宣告是由如 Module 或 Assembly 的選擇性屬性 (Attribute) 修飾詞 (Modifier)、屬性 (Attribute) 名稱、(可能多載的) 必要位置參數清單,以及選擇性 (Optional) 的具名引數清單所組成。您必須將具修飾詞的屬性 (Attribute) 置入位於原始程式檔 (Source File) 上方的屬性 (Attribute) 區段中。例如,以下程式碼設定了定義組件標題的組件屬性 (Attribute),另外還設定了指出模組可與 Common Language Specification (CLS) 相容的模組屬性 (Attribute):

Imports System.Reflection
<Assembly: AssemblyTitleAttribute("Production assembly 4"), _
Module: CLSCompliant(True)> 

組件屬性 (Attribute) 也可以透過 AssemblyInfo.vb 檔案套用,該檔案會自動由 Visual Studio 使用者介面加入專案。這個檔案包含組件層級 (Assembly-level) 的屬性 (Attribute),擁有預設值或空值。

當套用至如屬性 (Property) 之類的程式項目時,屬性 (Attribute) 會位於項目之前。例如,下列程式碼會將屬性 (Attribute) 套用至類別定義:

<CustomAttr(Update:=True)> Class Class1

依照慣例,所有屬性 (Attribute) 名稱都會以 "Attribute" 一字做為結尾,以區別屬性與 .NET Framework 中的其他項目。然而,在使用屬性 (Attribute) 時,您並不需要指定屬性 (Attribute) 尾碼。例如,如果您已有一個名為 CustomAttrAttribute 的屬性 (Attribute),則指定 <CustomAttr(Update:=True)> 就相當於指定 <CustomAttrAttribute(Update:=True)>。

屬性 (Attribute) 引數

屬性 (Attribute) 使用選擇性的、必要的、位置和具名引數的方式,大都與物件使用這些引數型別的方式相同。位置引數是依照其為屬性 (Attribute) 建構函式 (Constructor) 宣告的順序來指定的引數。例如,下列程式碼會呼叫含兩個值的屬性 (Attribute) 的 Sub New 建構函式:

<CustomAttr(True, False)> Class Class1

屬性 (Attribute) 類別中傳給 Sub New 的引數通常用來初始化欄位及屬性 (Property) 值。

您可以使用具名引數來直接設定屬性 (Property) 及欄位的值。指定具名引數的方式是在引數名稱之後附加「:=」,後面再加上要指定的值。和位置引數不同,具名引數可用任何順序來指定。例如,下列程式碼會將 Update 欄位設為 True,且將 Keep 欄位設為 False:

<CustomAttr(Update:=True, Keep:=False)> Class Class1
注意事項:

屬性 (Attribute) 引數和與標準方法呼叫一起使用的引數間有一個重要的差異,您必須使用與屬性類別的 Sub New 建構函式一起使用的引數的 Positional 引數。具名引數只能用在設定欄位值和屬性類別的屬性。

必要引數指一定要指定的引數。選擇性引數是指可以略過的引數:使用 Positional 引數時,可用逗號做為替代符號來略過;而使用具名引數時,則可直接省略。

屬性引數必須是常數運算式。

屬性 (Attribute) 範例

下列程序提供屬性 (Attribute) 宣告的範例:

若要使用 MarshalAs 屬性 (Attribute) 來控制參數的封送處理方式

  1. System.Runtime.InteropServices 命名空間將 Imports 陳述式加入原始程式碼的最上方:

    Imports System.Runtime.InteropServices
    
  2. MarshalAsAttribute 屬性 (Attribute) 做為參數前置字元,並指定目標需要的資料型別。例如,若是 Windows API 函式,下列程式碼會將兩個參數封送處理為對字串的長指標 (LPStr) 資料型別:

    Declare Auto Sub CopyFile Lib "Kernel32.Lib" ( _
       <MarshalAs(UnmanagedType.LPWStr)> ByVal existingfile As String, _
       <MarshalAs(UnmanagedType.LPWStr)> ByVal newfile As String, _
       ByVal failifexists As Boolean _
    )
    

Common Language Runtime 會使用 MarshalAsAttribute 屬性 (Attribute) 來判斷應該如何在 Visual Basic 的 Managed 程式碼和 Windows API 呼叫的 Unmanaged 程式碼之間封送處理參數。

若要將方法公開至遠端 Web 用戶端

  1. 在 [檔案] 功能表中,按一下 [專案],選擇 [ASP.NET Web 服務] 範本,並加入 System.Web 命名空間的 Imports 陳述式:

    Imports System.Web.Services
    
  2. 定義方法,並使用 WebMethodAttribute 屬性 (Attribute) 讓方法可從遠端 Web 用戶端呼叫。

    <WebMethod()> Public Function HelloWorld() As String
        HelloWorld = "Hello World..."
    End Function
    

將 XML Web Service 的方法建立成 Public 還不足以將它公開至 Web 用戶端。您必須將 WebMethodAttribute 屬性 (Attribute) 明確套用至方法,遠端 Web 用戶端才能呼叫這個方法。

請參閱

工作

HOW TO:定義您自己的屬性

概念

Visual Basic 中的屬性概觀

屬性的常見用法

依位置和名稱傳遞引數

參考

WebMethodAttribute

Imports 陳述式 (.NET 命名空間和型別)

MarshalAsAttribute

System.Web

其他資源

Visual Basic 中的自訂屬性