Export (0) Print
Expand All

DesignerSerializationVisibilityAttribute Class

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

For a list of all members of this type, see DesignerSerializationVisibilityAttribute Members.

System.Object
   System.Attribute
      System.ComponentModel.DesignerSerializationVisibilityAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Method Or _
   AttributeTargets.Property)>
NotInheritable Public Class _
   DesignerSerializationVisibilityAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Method |
   AttributeTargets.Property)]
public sealed class DesignerSerializationVisibilityAttribute :
   Attribute
[C++]
[AttributeUsage(AttributeTargets::Method |
   AttributeTargets::Property)]
public __gc __sealed class
   DesignerSerializationVisibilityAttribute : public Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Method | AttributeTargets.Property)
class DesignerSerializationVisibilityAttribute extends
   Attribute

Thread Safety

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

Remarks

When a serializer persists the persistable state of a design mode document, it often adds code to the initialization method of components in order 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.

The DesignerSerializationVisibilityAttribute allows you to 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.

Example

[Visual Basic, C#, C++] The following example demonstrates use of a DesignerSerializationVisibilityAttribute set to Content, to persist 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 in a new project to the compiled .DLL file, add the control from the .DLL file to the Toolbox in design mode by right clicking the toolbox, selecting Customize Toolbox..., and selecting the control from the .DLL file containing the user control. Then drag the control from the toolbox to a Form, and set the properties of the DimensionData object listed in the property grid while the control is selected. When you view the code for the form, code will have been added to the InitializeComponent method of the parent form of the control which sets the values of the properties of the control to those which you have set in design mode.

[Visual Basic] 
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 Not (components Is 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

[C#] 
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
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 : System.Windows.Forms.UserControl
    {
    private System.ComponentModel.Container components = null;                
    
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public DimensionData Dimensions
    {
        get 
        {
        return new DimensionData(this);
        }        
    }

    public ContentSerializationExampleControl()
    {
            InitializeComponent();        
    }
        
    protected override void Dispose( bool disposing )
    {
        if( disposing )
        {
        if( components != null )
            components.Dispose();
        }
        base.Dispose( disposing );
    }

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

    [TypeConverterAttribute(typeof(System.ComponentModel.ExpandableObjectConverter))]
    // This attribute indicates that the public properties of this object should be listed in the property grid.
    public class DimensionData
    {        
    private 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
        {
        return owner.Location;
        }
        set
        {
        owner.Location = value;
        }
    }

    public Size FormSize
    {
        get
            {
        return owner.Size;
        }
        set
        {
        owner.Size = value;
        }
    }
    }
}

[C++] 
#using <mscorlib.dll>
#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;

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

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

public:
    __property Point get_Location()
    {
        return owner->Location;
    }
    __property void set_Location( Point value )
    {
        owner->Location = value;
    }

    __property Size get_FormSize()
    {
        return owner->Size;
    }
    __property void set_FormSize( 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 __gc class ContentSerializationExampleControl : public System::Windows::Forms::UserControl
{
private:
    System::ComponentModel::Container* components;                

public:
    [DesignerSerializationVisibility(DesignerSerializationVisibility::Content)]
    __property DimensionData* get_Dimensions() 
    {
        return new DimensionData(this);
    }        

    ContentSerializationExampleControl()
    {
        InitializeComponent();        
    }

protected:
    void Dispose( bool disposing )
    {
        if( disposing )
        {
            if( components != 0 )
                components->Dispose();
        }
        UserControl::Dispose( disposing );
    }

private:
    void InitializeComponent()
    {
        components = new System::ComponentModel::Container();
    }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.ComponentModel

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

DesignerSerializationVisibilityAttribute Members | System.ComponentModel Namespace | Attribute | PropertyDescriptor | AttributeCollection | PropertyDescriptorCollection

Show:
© 2014 Microsoft