次の方法で共有


ShouldSerialize メソッドと Reset メソッドを使用した既定値の定義

ShouldSerializeReset は、プロパティに単純な既定値がない場合に、プロパティに指定できる省略可能なメソッドです。 プロパティに単純な既定値がある場合は、DefaultValueAttribute を適用し、代わりに属性クラスコンストラクターに既定値を指定する必要があります。 これらのメカニズムのいずれかを使用すると、デザイナーで次の機能が有効になります。

  • このプロパティは、既定値から変更された場合に、プロパティ ブラウザーで視覚的な表示を提供します。

  • ユーザーは、プロパティを右クリックし、[ リセット] を選択して、プロパティを既定値に戻すことができます。

  • デザイナーは、より効率的なコードを生成します。

手記

を適用するか、または PropertyName と メソッド、並びに PropertyName と メソッドを提供します。 両方を使用しないでください。

ShouldSerialize または Reset メソッドを宣言するときは、private アクセス修飾子を使用します。 これらのメソッドは通常、ユーザー コードではなくデザイナーによって呼び出されます。

ResetPropertyName メソッドは、次のコード フラグメントに示すように、プロパティを既定値に設定します。

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

手記

プロパティに Reset メソッドがない場合、DefaultValueAttributeでマークされておらず、宣言に既定値が指定されていない場合、そのプロパティの Reset オプションは、Visual Studio の Windows フォーム デザイナーの [プロパティ] ウィンドウのショートカット メニューで無効になります。

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

ヒント

プロパティがデザイナーによってシリアル化されないようにするには、DesignerSerializationVisibility 属性を Hiddenの値と共に追加します。

完全なコード例を次に示します。

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表示されません。代わりに、親の Font プロパティ (nullでない場合) または Controlで定義されている既定の Font 値が表示されます。 したがって、MyFont の既定値を単に設定することはできません。また、このプロパティに DefaultValueAttribute を適用することはできません。 代わりに、MyFont プロパティに対して ShouldSerialize メソッドと Reset メソッドを実装する必要があります。

関連項目