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

ShouldSerialize ve Reset , özelliği basit bir varsayılan değere sahip değilse, bir özellik için sağlayabilirsiniz isteğe bağlı yöntemlerdir. Özelliğin basit bir varsayılan değeri varsa, bunu uygulamalı ve bunun yerine öznitelik sınıfı DefaultValueAttribute oluşturucusu için varsayılan değeri s uygulamalıdır. Bu mekanizmalardan biri tasarımcıda aşağıdaki özellikleri sağlar:

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

  • Kullanıcı özelliğine sağ tıklar ve Özelliği varsayılan değerine geri yüklemek için Sıfırla'yı seçebilir.

  • Tasarımcı daha verimli kod üretir.

Not

DefaultValueAttributeResetDefaultValueAttribute ve ShouldSerializeReset girin. Her ikisini birden kullanma.

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

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

Not

Bir özelliğin yöntemi yoksa, ile işaretlanmaz ve bildiriminde sağlanan varsayılan değere sahip değildir, bu özelliğin seçeneği, Visual Studio'daki Windows Forms Tasarımcısı'nın Özellikler penceresinin kısayol menüsünde devre ResetDefaultValueAttribute dışı Reset bırakılır. Reset

Visual Studio gibi tasarımcılar, özelliğin varsayılan değerinden değişip değişmediğini kontrol etmek için PropertyName yöntemini kullanır ve yalnızca bir özellik değiştirilse forma kod yazarak daha verimli kod oluşturma ShouldSerializeShouldSerialize sağlar. Örneğin:

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

İpucu

Bir özelliğin tasarımcı tarafından seri hale getirmesini kalıcı olarak engellemek için DesignerSerializationVisibility özniteliğini değeriyle ekleyin.

Eksiksiz bir kod örneği aşağıdaki gibidir.

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 erişilen özel değişkenin değeri olsa bile, özellik tarayıcısı görüntülemez; bunun yerine, üst değişkenin özelliğini ( değilse veya içinde tanımlanan varsayılan MyFontnullnullFontnullFont değeri) Control görüntüler. Bu nedenle için varsayılan MyFont değer basitçe ayar olamaz ve bu DefaultValueAttribute özelikte uygulanamaz. Bunun ShouldSerialize yerine, Reset özelliği için ve yöntemlerinin uygulanması MyFont gerekir.

Ayrıca bkz.