DesignerSerializationVisibilityAttribute Class
TOC
Collapse the table of content
Expand the table of content

DesignerSerializationVisibilityAttribute Class

Specifies the type of persistence to use when serializing a property on a component at design time.

Namespace: System.ComponentModel
Assembly: System (in system.dll)

'Declaration
<AttributeUsageAttribute(AttributeTargets.Method Or AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Event)> _
Public NotInheritable Class DesignerSerializationVisibilityAttribute
	Inherits Attribute
'Usage
Dim instance As DesignerSerializationVisibilityAttribute

/** @attribute AttributeUsageAttribute(AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Event) */ 
public final class DesignerSerializationVisibilityAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Event) 
public final class DesignerSerializationVisibilityAttribute extends Attribute
Not applicable.

When a serializer persists the persistable state of a design mode document, it often adds code to the initialization method of components to persist values of properties that have been set at design time. This happens by default for most basic types, if no attribute has been set to direct other behavior.

With the DesignerSerializationVisibilityAttribute, you can indicate whether the value for a property is Visible, and should be persisted in initialization code, Hidden, and should not be persisted in initialization code, or consists of Content, which should have initialization code generated for each public, not hidden property of the object assigned to the property.

Members that do not have a DesignerSerializationVisibilityAttribute will be treated as though they have a DesignerSerializationVisibilityAttribute with a value of Visible. The values of a property marked as Visible will be serialized, if possible, by a serializer for the type. To specify custom serialization for a particular type or property, use the DesignerSerializerAttribute.

For more information, see Attributes Overview and Extending Metadata Using Attributes.

The following code example demonstrates the use of a DesignerSerializationVisibilityAttribute set to Content. It persists the values of a public property of a user control, which can be configured at design time. To use the example, first compile the following code into a user control library. Next, add a reference to the compiled .dll file in a new Windows Application project. If you are using Visual Studio, the ContentSerializationExampleControl is automatically added to the Toolbox.

Drag the control from the Toolbox to a form, and set the properties of the DimensionData object listed in the Properties window. When you view the code for the form, code will have been added to the InitializeComponent method of the parent form. This code sets the values of the control's properties to those which you have set in design mode.

Imports System
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 'New


        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 'Dispose


        Private Sub InitializeComponent()
        End Sub 'InitializeComponent
    End Class 'ContentSerializationExampleControl

    ' 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 'New


        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 'DimensionData
End Namespace 'DesignerSerializationVisibilityTest

package DesignerSerializationVisibilityTest;

import System.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.ComponentModel.Design.*;
import System.Drawing.*;
import System.Windows.Forms.*;

// 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
   extends System.Windows.Forms.UserControl
{
    private System.ComponentModel.Container components = null;

    /** @attribute DesignerSerializationVisibility(
         DesignerSerializationVisibility.Content)
     */
    /** @property 
     */
    public DimensionData get_Dimensions()
    {
        return new DimensionData(this);
    } //get_Dimensions

    public ContentSerializationExampleControl()
    {
        InitializeComponent();
    } //ContentSerializationExampleControl

    protected void Dispose(boolean disposing)
    {
        if (disposing) {
            if (components != null) {
                components.Dispose();
            }
        }
        super.Dispose(disposing);
    } //Dispose

    private void InitializeComponent()
    {
        components = new System.ComponentModel.Container();
    } //InitializeComponent
} //ContentSerializationExampleControl

/** @attribute TypeConverterAttribute(System.ComponentModel.
     ExpandableObjectConverter.class)
 */
public class DimensionData
{
    // This attribute indicates that the public properties of this 
    // object should be listed in the property grid.
    private Control owner;

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

    /** @property 
     */
    public Point get_Location()
    {
        return owner.get_Location();
    } //get_Location

    /** @property 
     */
    public void set_Location(Point value)
    {
        owner.set_Location(value);
    } //set_Location

    /** @property 
     */
    public Size get_FormSize()
    {
        return owner.get_Size();
    } //get_FormSize

    /** @property 
     */
    public void set_FormSize(Size value)
    {
        owner.set_Size(value);
    } //set_FormSize
} //DimensionData

System.Object
   System.Attribute
    System.ComponentModel.DesignerSerializationVisibilityAttribute

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2016 Microsoft