DesignerSerializationVisibilityAttribute Class

Definition

指定在设计时序列化组件上的属性时所使用的持久性类型。Specifies the type of persistence to use when serializing a property on a component at design time.

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
type DesignerSerializationVisibilityAttribute = class
    inherit Attribute
Public NotInheritable Class DesignerSerializationVisibilityAttribute
Inherits Attribute
Inheritance
DesignerSerializationVisibilityAttribute
Attributes

Examples

下面的代码示例演示如何使用设置为 ContentDesignerSerializationVisibilityAttributeThe 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. 接下来,在新的 Windows 应用程序项目中添加对已编译 .dll 文件的引用。Next, add a reference to the compiled .dll file in a new Windows Application project. 如果使用的是 Visual Studio,则会将 ContentSerializationExampleControl 自动添加到 "工具箱"。If you are using Visual Studio, the ContentSerializationExampleControl is automatically added to the Toolbox.

将控件从 "工具箱" 拖动到窗体,并设置属性窗口中列出的 DimensionData 对象的属性。Drag the control from the Toolbox to a form, and set the properties of the DimensionData object listed in the Properties window. 查看窗体的代码时,代码将添加到父窗体的 InitializeComponent 方法。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.

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

Remarks

当序列化程序保持设计模式文档的持久状态时,它通常会将代码添加到组件的初始化方法,以持久保存在设计时设置的属性值。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.

使用 DesignerSerializationVisibilityAttribute,你可以指示属性的值是否 Visible,并应在初始化代码中保留,Hidden,并且不应在初始化代码中保留,也不能包含 Content,其中应为分配给属性的对象的每个公共、非隐藏属性生成初始化代码。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.

不具有 DesignerSerializationVisibilityAttribute 的成员将被视为具有值为 VisibleDesignerSerializationVisibilityAttributeMembers that do not have a DesignerSerializationVisibilityAttribute will be treated as though they have a DesignerSerializationVisibilityAttribute with a value of Visible. 标记为 Visible 的属性的值将在可能的情况下通过类型的序列化程序进行序列化。The values of a property marked as Visible will be serialized, if possible, by a serializer for the type. 若要为特定类型或属性指定自定义序列化,请使用 DesignerSerializerAttributeTo specify custom serialization for a particular type or property, use the DesignerSerializerAttribute.

有关更多信息,请参阅特性For more information, see Attributes.

Constructors

DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility)

使用指定的 DesignerSerializationVisibility 值初始化 DesignerSerializationVisibilityAttribute 类的新实例。Initializes a new instance of the DesignerSerializationVisibilityAttribute class using the specified DesignerSerializationVisibility value.

Fields

Content

指定序列化程序应该序列化属性的内容,而不是属性本身。Specifies that a serializer should serialize the contents of the property, rather than the property itself. 此字段为只读。This field is read-only.

Default

指定默认值为 Visible,也就是说,可视化设计器使用默认规则来生成属性值。Specifies the default value, which is Visible, that is, a visual designer uses default rules to generate the value of a property. static 字段是只读的。This static field is read-only.

Hidden

指定序列化程序不应该序列化属性的值。Specifies that a serializer should not serialize the value of the property. static 字段是只读的。This static field is read-only.

Visible

指定应该允许序列化程序序列化属性的值。Specifies that a serializer should be allowed to serialize the value of the property. static 字段是只读的。This static field is read-only.

Properties

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)
Visibility

获取一个值,该值指示序列化程序在确定是否以及如何保持某个属性的值时应使用的基本序列化模式。Gets a value indicating the basic serialization mode a serializer should use when determining whether and how to persist the value of a property.

Methods

Equals(Object)

指示此实例和指定的对象是否相等。Indicates whether this instance and a specified object are equal.

GetHashCode()

返回该对象的哈希代码。Returns the hash code for this object.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute()

获取一个值,该值指示该特性的当前值是否为该特性的默认值。Gets a value indicating whether the current value of the attribute is the default value for the attribute.

IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

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

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

Applies to

See also