ShouldSerialize ile Varsayılan Değerleri Tanımlama ve Yöntemleri Sıfırlama

ShouldSerialize ve Reset özelliğin basit bir varsayılan değeri yoksa, bir özellik için sağlayabileceğiniz isteğe bağlı yöntemlerdir. Özelliğin basit bir varsayılan değeri varsa, öğesini uygulamanız DefaultValueAttribute ve bunun yerine öznitelik sınıfı oluşturucusunun varsayılan değerini sağlamanız gerekir. Bu mekanizmalardan biri tasarımcıda aşağıdaki özellikleri etkinleştirir:

  • özelliği, varsayılan değerinden değiştirilmişse özellik tarayıcısında görsel gösterge sağlar.

  • Kullanıcı özelliğe sağ tıklayıp Sıfırla'yı seçerek özelliği varsayılan değerine geri yükleyebilir.

  • Tasarımcı daha verimli kod oluşturur.

Dekont

veya uygulayın DefaultValueAttribute veya PropertyName veShouldSerialize PropertyName yöntemlerini sağlayınReset. Her ikisini de kullanmayın.

Veya ShouldSerializeReset yöntemini bildirirken erişim değiştiricisini private kullanın. Bu yöntemler genellikle kullanıcı kodu tarafından değil tasarımcı tarafından çağrılır.

ResetPropertyName yöntemi, aşağıdaki kod parçasında gösterildiği gibi bir özelliği varsayılan değerine ayarlar.

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

Dekont

Bir özelliğin yöntemi Reset yoksa, ile DefaultValueAttributeişaretlenmemişse ve bildiriminde sağlanan varsayılan bir değer yoksa, bu özelliğin Reset seçeneği Visual Studio'daki Windows Forms Tasarım Aracı Özellikler penceresinin kısayol menüsünde devre dışı bırakılır.

Visual Studio ShouldSerialize gibi Tasarım AracıÖzelliğin varsayılan değerinden değişip değişmediğini denetlemek için PropertyName yöntemi ve yalnızca bir özellik değiştirildiğinde forma kod yazma, böylece daha verimli kod oluşturma olanağı sağlar. Örnek:

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

Bahşiş

Bir özelliğin tasarımcı tarafından seri hale getirilmesini kalıcı olarak önlemek istiyorsanız, değeriyle HiddenTasarım Aracı SerializationVisibility özniteliğini ekleyin.

Tam bir kod örneği aşağıda verilmiştir.

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

Bu durumda, özelliği tarafından MyFont erişilen özel değişkenin değeri olsa bile, özellik tarayıcısı görüntülenmeznull; bunun yerine üst değişkenin özelliği (değilsenull) veya içinde Controltanımlanan varsayılan Font değeri görüntüler Fontnull. Bu nedenle için MyFont varsayılan değer basitçe ayarlanamaz ve DefaultValueAttribute bu özelliğe uygulanamaz. Bunun yerine özelliği ShouldSerialize için MyFont ve Reset yöntemleri uygulanmalıdır.

Ayrıca bkz.