Partager via


DesignerSerializationVisibilityAttribute Classe

Définition

Spécifie le type de persistance à utiliser lors de la sérialisation d’une propriété sur un composant au moment du design.

public ref class DesignerSerializationVisibilityAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Method | System.AttributeTargets.Property)]
public sealed class DesignerSerializationVisibilityAttribute : Attribute
public sealed class DesignerSerializationVisibilityAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property)]
public sealed class DesignerSerializationVisibilityAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Method | System.AttributeTargets.Property)>]
type DesignerSerializationVisibilityAttribute = class
    inherit Attribute
type DesignerSerializationVisibilityAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property)>]
type DesignerSerializationVisibilityAttribute = class
    inherit Attribute
Public NotInheritable Class DesignerSerializationVisibilityAttribute
Inherits Attribute
Héritage
DesignerSerializationVisibilityAttribute
Attributs

Exemples

L’exemple de code suivant illustre l’utilisation d’un DesignerSerializationVisibilityAttribute jeu sur Content. Elle conserve les valeurs d’une propriété publique d’un contrôle utilisateur, qui peut être configurée au moment du design. Pour utiliser l’exemple, compilez d’abord le code suivant dans une bibliothèque de contrôles utilisateur. Ensuite, ajoutez une référence au fichier de .dll compilé dans un nouveau projet d’application Windows. Si vous utilisez Visual Studio, le ContentSerializationExampleControl est automatiquement ajouté au Toolbox.

Faites glisser le contrôle du Toolbox vers un formulaire et définissez les propriétés de l’objet DimensionData répertorié dans le Fenêtre Propriétés. Lorsque vous affichez le code du formulaire, le code a été ajouté à la InitializeComponent méthode du formulaire parent. Ce code définit les valeurs des propriétés du contrôle sur celles que vous avez définies en mode création.

#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Drawing;
using namespace System::Windows::Forms;

// This attribute indicates that the public properties of this object should be listed in the property grid.

[TypeConverterAttribute(System::ComponentModel::ExpandableObjectConverter::typeid)]
public ref class DimensionData
{
private:
   Control^ owner;

internal:

   // This class reads and writes the Location and Size properties from the Control which it is initialized to.
   DimensionData( Control^ owner )
   {
      this->owner = owner;
   }

public:

   property Point Location 
   {
      Point get()
      {
         return owner->Location;
      }

      void set( Point value )
      {
         owner->Location = value;
      }

   }

   property Size FormSize 
   {
      Size get()
      {
         return owner->Size;
      }

      void set( Size value )
      {
         owner->Size = value;
      }
   }
};

// The code for this user control declares a public property of type DimensionData with a DesignerSerializationVisibility 
// attribute set to DesignerSerializationVisibility.Content, indicating that the properties of the object should be serialized.
// The public, not hidden properties of the object that are set at design time will be persisted in the initialization code
// for the class object. Content persistence will not work for structs without a custom TypeConverter.  
public ref class ContentSerializationExampleControl: public System::Windows::Forms::UserControl
{
private:
   System::ComponentModel::Container^ components;

public:

   property DimensionData^ Dimensions 
   {
      [DesignerSerializationVisibility(DesignerSerializationVisibility::Content)]
      DimensionData^ get()
      {
         return gcnew DimensionData( this );
      }
   }
   ContentSerializationExampleControl()
   {
      InitializeComponent();
   }

public:
   ~ContentSerializationExampleControl()
   {
      if ( components != nullptr )
      {
         delete components;
      }
   }

private:
   void InitializeComponent()
   {
      components = gcnew System::ComponentModel::Container;
   }
};
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace DesignerSerializationVisibilityTest;

// The code for this user control declares a public property of type DimensionData with a DesignerSerializationVisibility
// attribute set to DesignerSerializationVisibility.Content, indicating that the properties of the object should be serialized.

// The public, not hidden properties of the object that are set at design time will be persisted in the initialization code
// for the class object. Content persistence will not work for structs without a custom TypeConverter.

public class ContentSerializationExampleControl : UserControl
{
    Container components;

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public DimensionData Dimensions => new(this);

    public ContentSerializationExampleControl() => InitializeComponent();

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            components?.Dispose();
        }
        base.Dispose(disposing);
    }

    void InitializeComponent() => components = new Container();
}

[TypeConverter(typeof(ExpandableObjectConverter))]
// This attribute indicates that the public properties of this object should be listed in the property grid.
public class DimensionData
{
    readonly Control owner;

    // This class reads and writes the Location and Size properties from the Control which it is initialized to.
    internal DimensionData(Control owner) => this.owner = owner;

    public Point Location
    {
        get => owner.Location;
        set => owner.Location = value;
    }

    public Size FormSize
    {
        get => owner.Size;
        set => owner.Size = value;
    }
}
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Windows.Forms

Namespace DesignerSerializationVisibilityTest
    _
    ' The code for this user control declares a public property of type DimensionData with a DesignerSerializationVisibility 
    ' attribute set to DesignerSerializationVisibility.Content, indicating that the properties of the object should be serialized.

    ' The public, not hidden properties of the object that are set at design time will be persisted in the initialization code
    ' for the class object. Content persistence will not work for structs without a custom TypeConverter.		
    Public Class ContentSerializationExampleControl
        Inherits System.Windows.Forms.UserControl
        Private components As System.ComponentModel.Container = Nothing


        <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
        Public ReadOnly Property Dimensions() As DimensionData
            Get
                Return New DimensionData(Me)
            End Get
        End Property


        Public Sub New()
            InitializeComponent()
        End Sub


        Protected Overloads Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If (components IsNot Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub


        Private Sub InitializeComponent()
        End Sub
    End Class

    ' This attribute indicates that the public properties of this object should be listed in the property grid.
   <TypeConverterAttribute(GetType(System.ComponentModel.ExpandableObjectConverter))> _   
    Public Class DimensionData
        Private owner As Control

        ' This class reads and writes the Location and Size properties from the Control which it is initialized to.
        Friend Sub New(ByVal owner As Control)
            Me.owner = owner
        End Sub


        Public Property Location() As Point
            Get
                Return owner.Location
            End Get
            Set(ByVal Value As Point)
                owner.Location = Value
            End Set
        End Property


        Public Property FormSize() As Size
            Get
                Return owner.Size
            End Get
            Set(ByVal Value As Size)
                owner.Size = Value
            End Set
        End Property
    End Class
End Namespace 'DesignerSerializationVisibilityTest

Remarques

Lorsqu’un sérialiseur conserve l’état persistant d’un document en mode création, il ajoute souvent du code à la méthode d’initialisation des composants pour conserver les valeurs des propriétés définies au moment du design. Cela se produit par défaut pour la plupart des types de base, si aucun attribut n’a été défini sur un autre comportement.

Avec le DesignerSerializationVisibilityAttribute, vous pouvez indiquer si la valeur d’une propriété est Visible, et doit être conservée dans le code d’initialisation, Hiddenet ne doit pas être conservée dans le code d’initialisation, ou se compose de Content, qui doit avoir du code d’initialisation généré pour chaque propriété publique, non masquée de l’objet affecté à la propriété.

Les membres qui n’ont pas de DesignerSerializationVisibilityAttribute volonté sont traités comme s’ils ont une DesignerSerializationVisibilityAttribute valeur de Visible. Les valeurs d’une propriété marquée comme Visible sérialisée, si possible, par un sérialiseur pour le type. Pour spécifier la sérialisation personnalisée pour un type ou une propriété particulier, utilisez le DesignerSerializerAttribute.

Pour plus d’informations, consultez Attributs.

Constructeurs

Nom Description
DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility)

Initialise une nouvelle instance de la classe à l’aide DesignerSerializationVisibilityAttribute de la valeur spécifiée DesignerSerializationVisibility .

Champs

Nom Description
Content

Spécifie qu’un sérialiseur doit sérialiser le contenu de la propriété, plutôt que la propriété elle-même. Ce champ est en lecture seule.

Default

Spécifie la valeur par défaut, autrement Visibledit, un concepteur visuel utilise des règles par défaut pour générer la valeur d’une propriété. Ce static champ est en lecture seule.

Hidden

Spécifie qu’un sérialiseur ne doit pas sérialiser la valeur de la propriété. Ce static champ est en lecture seule.

Visible

Spécifie qu’un sérialiseur doit être autorisé à sérialiser la valeur de la propriété. Ce static champ est en lecture seule.

Propriétés

Nom Description
TypeId

En cas d’implémentation dans une classe dérivée, obtient un identificateur unique pour cette Attribute.

(Hérité de Attribute)
Visibility

Obtient une valeur indiquant le mode de sérialisation de base qu’un sérialiseur doit utiliser pour déterminer si et comment conserver la valeur d’une propriété.

Méthodes

Nom Description
Equals(Object)

Indique si cette instance et un objet spécifié sont égaux.

GetHashCode()

Retourne le code de hachage de cet objet.

GetType()

Obtient la Type de l’instance actuelle.

(Hérité de Object)
IsDefaultAttribute()

Obtient une valeur indiquant si la valeur actuelle de l’attribut est la valeur par défaut de l’attribut.

Match(Object)

En cas de substitution dans une classe dérivée, retourne une valeur qui indique si cette instance est égale à un objet spécifié.

(Hérité de Attribute)
MemberwiseClone()

Crée une copie superficielle du Objectactuel.

(Hérité de Object)
ToString()

Retourne une chaîne qui représente l’objet actuel.

(Hérité de Object)

Implémentations d’interfaces explicites

Nom Description
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Mappe un jeu de noms avec un jeu correspondant d'identificateurs de dispatch.

(Hérité de Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Récupère les informations de type d’un objet, qui peuvent être utilisées pour obtenir les informations de type d’une interface.

(Hérité de Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Récupère le nombre d'interfaces d'informations de type fourni par un objet (0 ou 1).

(Hérité de Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Fournit l’accès aux propriétés et méthodes exposées par un objet.

(Hérité de Attribute)

S’applique à

Voir aussi