ShouldSerialize 및 Reset 메서드를 사용하여 기본값 정의

ShouldSerializeReset 속성에 간단한 기본값이 없는 경우 속성을 제공할 수 있는 선택적 메서드입니다. 속성에 간단한 기본값이 있는 경우 DefaultValueAttribute을(를) 적용하고 대신 특성 클래스 생성자에 기본값을 제공해야 합니다. 이러한 메커니즘 중 하나를 사용하면 디자이너에서 다음 기능을 사용할 수 있습니다.

  • 속성이 기본값에서 수정된 경우 속성 브라우저에서 시각적 표시를 제공합니다.

  • 사용자는 속성을 마우스 오른쪽 단추로 클릭하고 초기화를 선택하여 속성을 기본값으로 복원할 수 있습니다.

  • 디자이너는 보다 효율적인 코드를 생성합니다.

참고

DefaultValueAttribute을(를) 적용하거나 ResetPropertyNameShouldSerializePropertyName 메서드를 제공합니다. 둘 다 사용하지 마세요.

ShouldSerialize 또는 Reset 메서드를 선언할 때 private 액세스 한정자를 사용합니다. 이러한 메서드는 일반적으로 사용자 코드가 아닌 디자이너에 의해 호출됩니다.

ResetPropertyName 메서드는 다음 코드 조각과 같이 속성을 기본값으로 설정합니다.

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

참고

속성에 Reset 메서드가 없으며, DefaultValueAttribute(으)로 표시되지 않고, 해당 선언에 제공된 기본값이 없는 경우 해당 속성에 대한 Reset 옵션은 Visual Studio Windows Forms Designer의 속성 창 바로 가기 메뉴에서 사용하지 않도록 설정됩니다.

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 속성에 대해 ShouldSerializeReset 메서드를 구현해야 합니다.

참고 항목