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

ShouldSerializeReset 是選用方法,如果屬性沒有簡單的預設值,您可以提供屬性。 如果屬性具有簡單的預設值,您應該套用 DefaultValueAttribute,並改為將預設值提供給屬性類別建構函式。 這其中一種機制可在設計工具中啟用下列功能:

  • 屬性在屬性瀏覽器中提供視覺化指示,如果已經從其預設值修改。

  • 使用者可以在屬性上按右鍵,然後選擇 [重設],將屬性還原為其預設值。

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

注意

套用 DefaultValueAttribute,或提供 ResetPropertyNameShouldSerializePropertyName 方法。 請勿同時使用兩者。

宣告 ShouldSerializeReset 方法時,請使用 private 存取修飾元。 這些方法通常是由設計工具叫用,而不是由使用者程式碼叫用。

ResetPropertyName 方法會將屬性設定為其預設值,如下列代碼段所示。

Private Sub ResetMyFont()
   MyFont = Nothing
End Sub
private void ResetMyFont()
{
   MyFont = null;
}

注意

如果屬性沒有 Reset 方法,則不會以 DefaultValueAttribute 標示,而且若沒有在其宣告中提供的預設值,則會在 Visual Studio 中 Windows Form Designer 的 [屬性] 視窗捷徑功能表中停用該屬性的 [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.
Private Function ShouldSerializeMyFont() As Boolean
   Return thefont IsNot 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.
private bool ShouldSerializeMyFont()
{
   return thefont != null;
}

提示

如果您想要永久防止屬性由設計工具序列化,請使用 Hidden 的值,新增 DesignerSerializationVisibility 属性。

接下來會是完整的程式碼範例。

Option Explicit
Option Strict

Imports System.Drawing
Imports System.Windows.Forms

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

   Private Function ShouldSerializeMyFont() As Boolean
      Return thefont IsNot Nothing
   End Function

   Private Sub ResetMyFont()
      MyFont = Nothing
   End Sub
End Class
using System;
using System.Drawing;
using System.Windows.Forms;

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;
      }
   }

   private bool ShouldSerializeMyFont()
   {
      return thefont != null;
   }

   private void ResetMyFont()
   {
      MyFont = null;
   }
}

在此情況下,即使 MyFont 屬性存取的私用變數值為 null 時,屬性瀏覽器也不會顯示 null;相反地,如果它不是 null,則會顯示父系的 Font 屬性,或 Control 中定義的預設 Font 值。 因此,無法直接設定 MyFont 的預設值,而且無法將 DefaultValueAttribute 套用至此屬性。 相反地,必須為 MyFont 屬性實作 ShouldSerializeReset 方法。

另請參閱