使用 ShouldSerialize 和 Reset 方法定義預設值

更新:2007 年 11 月

如果屬性沒有簡單的預設值,則 ShouldSerialize 和 Reset 是可以提供給屬性的選擇性方法。如果屬性具有簡單的預設值,您應該套用 DefaultValueAttribute,並提供預設值給屬性類別建構函式。這兩個機制的其中之一會在設計工具中啟用下列功能:

  • 如果屬性已被修改而與預設值不同,則屬性會在屬性瀏覽器中提供視覺化的表示。

  • 使用者可以在屬性上按一下滑鼠右鍵,並選擇 [重設] 將屬性還原為預設值。

  • 設計工具會產生較有效率的程式碼。

    注意事項:

    套用 DefaultValueAttribute 或提供 ResetPropertyName 和 ShouldSerializePropertyName 方法。不要兩者都使用。

ResetPropertyName 方法設定屬性為其預設值,如下列程式碼片段所示。

Public Sub ResetMyFont()
   MyFont = Nothing
End Sub
public void ResetMyFont() {
   MyFont = null;
}
注意事項:

如果屬性沒有 Reset 方法、未標記為 DefaultValueAttribute,且在宣告中沒有提供預設值,則在 Visual Studio 中 Windows Form 設計工具的 [屬性] 視窗的快速鍵功能表內,會停用該屬性的 Reset 選項。

設計工具 (例如 Visual Studio) 會使用 ShouldSerializePropertyName 方法來檢查屬性的預設值是否已經變更,並且只有變更屬性時,才會將程式碼寫入表單中,因此能更有效率地產生程式碼。例如:

'Returns true if the font has changed; otherwise, returns false.
' The designer writes code to the form only if true is returned.
Public Function ShouldSerializeMyFont() As Boolean
   Return Not (thefont Is Nothing)
End Function
// Returns true if the font has changed; otherwise, returns false.
// The designer writes code to the form only if true is returned.
public bool ShouldSerializeMyFont() {
   return thefont != null;
}

完整程式碼範例如下。

Option Explicit
Option Strict

Imports System
Imports System.Windows.Forms
Imports System.Drawing

Public Class MyControl
   Inherits Control
   
   ' Declare an instance of the Font class
   ' and set its default value to Nothing.
   Private thefont As Font = Nothing
   
   ' The MyFont property. 
   Public Property MyFont() As Font
      ' Note that the Font property never
      ' returns null.
      Get
         If Not (thefont Is Nothing) Then
            Return thefont
         End If
         If Not (Parent Is Nothing) Then
            Return Parent.Font
         End If
         Return Control.DefaultFont
      End Get
      Set
         thefont = value
      End Set
   End Property
   
   Public Function ShouldSerializeMyFont() As Boolean
      Return Not (thefont Is Nothing)
   End Function
   
   Public Sub ResetMyFont()
      MyFont = Nothing
   End Sub
End Class
using System;
using System.Windows.Forms;
using System.Drawing;

public class MyControl : Control {
   // Declare an instance of the Font class
   // and set its default value to null.
   private Font thefont = null;
  
   // The MyFont property.    
   public Font MyFont {
      // Note that the MyFont property never
      // returns null.
      get {
         if (thefont != null) return thefont;
         if (Parent != null) return Parent.Font;
         return Control.DefaultFont;
      }
      set {
         thefont = value;
      }
   }

   public bool ShouldSerializeMyFont() {
      return thefont != null;
   }
 
   public void ResetMyFont() {
      MyFont = null;
   }
}

在這個案例中,即使當 MyFont 屬性所存取的私用變數為 null 時,屬性瀏覽器也不會顯示 null;相反地,如果它不是 null、或在 Control 中所定義的預設 Font 值,便會顯示父代 (Parent) 的 Font 屬性。因此,無法簡單設定 MyFont 的預設值,且無法套用 DefaultValueAttribute 至這個屬性,反而必須為 MyFont 屬性實作 ShouldSerialize 和 Reset 方法。

請參閱

概念

定義 Windows Form 控制項中的屬性

屬性變更事件

其他資源

Windows Form 控制項中的屬性