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

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

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

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

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

注意

套用 DefaultValueAttribute 或 提供 Reset PropertyName ShouldSerialize PropertyName 方法。 請勿同時使用兩者。

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

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

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

注意

如果屬性沒有 Reset 方法,則不會以 DefaultValueAttribute 標記 ,而且在其宣告中沒有提供預設值, Reset 則會在 Visual Studio 中 Windows Forms 設計工具的 [屬性 ] 視窗快捷方式功能表中 停用該屬性的選項。

Visual Studio 之類的設計工具會使用 ShouldSerialize PropertyName 方法來檢查屬性是否已經從預設值變更,並且只有在屬性變更時,才會將程式碼寫入表單中,從而允許產生更有效率的程式碼。 例如:

'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 ,屬性瀏覽器也不會顯示 ;相反地,如果屬性不是 ,則會顯示 Fontnull 父系的 屬性,如果不是 null ,或中所 Control 定義的預設值 Font 。 因此,無法直接設定 的 MyFont 預設值,而且 DefaultValueAttribute 無法套用至這個屬性。 相反地, ShouldSerialize 必須針對 屬性實作 MyFontReset 方法。

另請參閱