DesignerSerializationManager 类

定义

提供 IDesignerSerializationManager 接口的实现。Provides an implementation of the IDesignerSerializationManager interface.

public ref class DesignerSerializationManager : IServiceProvider, System::ComponentModel::Design::Serialization::IDesignerSerializationManager
public class DesignerSerializationManager : IServiceProvider, System.ComponentModel.Design.Serialization.IDesignerSerializationManager
type DesignerSerializationManager = class
    interface IDesignerSerializationManager
    interface IServiceProvider
Public Class DesignerSerializationManager
Implements IDesignerSerializationManager, IServiceProvider
继承
DesignerSerializationManager
实现

注解

IDesignerSerializationManager 接口旨在作为控制序列化的对象的格式无关接口。The IDesignerSerializationManager interface is designed to be a format-independent interface to an object that controls serialization. 它实质上是向序列化程序提供上下文和服务,实际上执行反序列化。It essentially provides context and services to serializers, which actually perform the deserialization. 通过跟踪对象,IDesignerSerializationManager 有助于反序列化过程。IDesignerSerializationManager assists in the deserialization process by keeping track of objects. 这在技术上与 IDesignerHost 接口类似:设计器实际上提供用户界面(UI),IDesignerHost 提供允许不同设计器一起工作的胶水。This is similar in technique to the IDesignerHost interface: designers actually provide the user interface (UI), and IDesignerHost provides the glue that allows different designers to work together.

DesignerSerializationManager 类实现 IDesignerSerializationManagerThe DesignerSerializationManager class implements IDesignerSerializationManager. 它旨在提供类似于运行时序列化程序(如 BinaryFormatter)的一般形式的反序列化。It is designed to provide a generic form of deserialization that is similar to run-time serializers like the BinaryFormatter.

DesignerSerializationManager 类实现了三个目标:The DesignerSerializationManager class achieves three goals:

  • 它是一个简单的全包式对象,可用于反序列化各种格式。It is a simple, turnkey object that can be used to deserialize a variety of formats.

  • 它是泛型的,不依赖于任何特定格式。It is generic and not tied to any particular format. 它可平均用于 CodeDOM 反序列化以及标记反序列化。It can be used equally for CodeDOM deserialization as well as markup deserialization.

  • 它是可扩展的,并支持复制/粘贴和撤消/重做方案中使用的不同序列化方法。It is extensible and supports different serialization methods that are used in copy/paste and undo/redo scenarios.

与运行时对象序列化相比,设计时序列化具有以下差异:Design-time serialization has the following differences from run-time object serialization:

  • 执行序列化的对象通常与运行时对象分离,以便可以从组件中删除设计时逻辑。The object performing the serialization is generally separate from the run-time object, so that design-time logic can be removed from a component.

  • 序列化方案假设将创建完全初始化的对象,并在反序列化过程中通过属性和方法调用对对象进行修改。The serialization scheme assumes the object will be created fully initialized, and then modified through property and method invocations during deserialization.

  • 不会序列化对象(属性包含默认值)上从未设置的值的对象的属性。Properties of an object that have values that were never set on the object (the properties contain the default values) are not serialized. 相反,反序列化流可能具有孔洞。Conversely, the deserialization stream may have holes.

  • 强调在序列化流中的内容质量上,而不是对象的完全序列化。Emphasis is placed on the quality of the content within the serialization stream, rather than the full serialization of an object. 这意味着,如果没有定义的序列化对象的方式,则可以跳过该对象,而不是引发异常。This means that if there is no defined way to serialize an object, that object may be skipped rather than throwing an exception. 序列化引擎可以在此处提供试探法来确定哪些失败是可忽略的,哪些失败是无法恢复的。The serialization engine may provide heuristics here to decide which failures can be ignored and which are unrecoverable.

  • 序列化流的数据可能比反序列化所需的数据多。The serialization stream may have more data than is needed for deserialization. 例如,源代码序列化将用户代码与反序列化对象图所需的代码混合在一起。Source code serialization, for example, has user code mixed in with the code needed to deserialize an object graph. 此用户代码必须在反序列化时忽略,并在序列化时保留。This user code must be ignored on deserialization and preserved on serialization.

由于存在这些差异,因此不同的序列化模型适用于设计时序列化。Because of these differences, a different serialization model applies to design-time serialization. 此模型为要序列化的每个数据类型使用单独的序列化程序对象。This model utilizes a separate serializer object for each data type being serialized. 每个序列化程序都将其作为一个整体提供给问题。Each serializer provides its small contribution to the problem as a whole. 这些序列化程序通过一个公共序列化管理器进行协调。These serializers are all coordinated through a common serialization manager. 序列化管理器负责在这些不同的序列化程序之间维护状态。The serialization manager is responsible for maintaining state between these different serializers. 例如,请看下面的类:As an example, consider the following class:

public class SampleObject
{
    private string stringValue = null;
    private int intValue = int.MinValue;

    public string StringProperty 
    { 
        get { return this.stringValue; }

        set { this.stringValue = value; }
    }

    public int IntProperty 
    {
        get { return this.intValue; }

        set{ this.intValue = value; }
    }
}
Public Class SampleObject
   Private stringValue As String = Nothing
   Private intValue As Integer = Integer.MinValue
   
   
   Public Property StringProperty() As String
      Get
         Return Me.stringValue
      End Get 
      Set
         Me.stringValue = value
      End Set
   End Property 
   
   Public Property IntProperty() As Integer
      Get
         Return Me.intValue
      End Get 
      Set
         Me.intValue = value
      End Set
   End Property
End Class 

此类的实例将使用三个不同的序列化程序:一个用于 SampleObject,一个用于字符串,另一个用于整数。An instance of this class would utilize three different serializers: one for SampleObject, one for strings, and another for integers. SampleObject 的序列化程序称为根序列化程序,因为 SampleObject 是序列化关系图的根。The serializer for SampleObject is called the root serializer because SampleObject is the root of the serialization graph. 还可以创建更复杂的对象图。More complex object graphs can be created as well. 例如,如果 SampleObject 进行了如下更改:For example, consider what would happen if SampleObject were changed as follows:

public class SampleObject
{
    private string stringValue = null;
    private int intValue = int.MinValue;
    private SampleObject childValue = null;

    public string StringProperty
    {
        get { return this.stringValue; }

        set { this.stringValue = value; }
    }

    public int IntProperty
    {
        get { return this.intValue; }

        set { this.intValue = value; }
    }

    public SampleObject Child
    {
        get { return this.childValue; }

        set { this.childValue = value; }
    }
}
Public Class SampleObject
   Private stringValue As String = Nothing
   Private intValue As Integer = Integer.MinValue
   Private childValue As SampleObject = Nothing
   
   
   Public Property StringProperty() As String
      Get
         Return Me.stringValue
      End Get 
      Set
         Me.stringValue = value
      End Set
   End Property 
   
   Public Property IntProperty() As Integer
      Get
         Return Me.intValue
      End Get 
      Set
         Me.intValue = value
      End Set
   End Property 
   
   Public Property Child() As SampleObject
      Get
         Return Me.childValue
      End Get 
      Set
         Me.childValue = value
      End Set
   End Property
End Class 

这允许 SampleObject 具有作为自身的另一个实例的子。This allows SampleObject to have a child that is another instance of itself. 以下代码填充对象图:The following code fills in the object graph:

class Program
{
    static void Main(string[] args)
    {
        SampleObject root = new SampleObject();

        SampleObject currentObject = root;

        for (int i = 0; i < 10; i++)
        {
            SampleObject o = new SampleObject();

            currentObject.Child = o;

            currentObject = o;
        }
    }
}
Class Program
   
   Public Overloads Shared Sub Main()
      Main(System.Environment.GetCommandLineArgs())
   End Sub
   
   Overloads Shared Sub Main(args() As String)
      Dim root As New SampleObject()
      
      Dim currentObject As SampleObject = root
      
      Dim i As Integer
      For i = 0 To 9
         Dim o As New SampleObject()
         
         currentObject.Child = o
         
         currentObject = o
      Next i
   End Sub 
End Class

序列化 root 时,将使用四个序列化程序:一个根序列化程序、一个用于子 SampleObject的序列化程序、一个用于 int的序列化程序和一个用于 string的序列化程序。When root is serialized, there will be four serializers used: one root serializer, one serializer for the child SampleObject, one serializer for int, and one serializer for string. 序列化程序根据类型进行缓存,因此不需要为 SampleObject的每个实例创建序列化程序。Serializers are cached based on type, so there is no need to create a serializer for each instance of SampleObject.

DesignerSerializationManager 类基于序列化会话的概念。The DesignerSerializationManager class is based on the idea of a serialization session. 会话维护可由各种序列化程序访问的状态。A session maintains state that can be accessed by the various serializers. 当释放会话时,此状态将被销毁。When a session is disposed, this state is destroyed. 这有助于确保序列化程序在很大程度上保持不变,并有助于清理已损坏的序列化程序。This helps to ensure that serializers remain largely stateless, and helps to clean up serializers that are have been corrupted. 下表说明如何在和会话之间管理状态。The following tables describe how state is managed in and among sessions.

全局状态Global State

此状态由序列化管理器对象拥有,但与当前序列化会话无关。This state is owned by the serialization manager object, but is independent of the current serialization session.

ObjectObject 用法Usage
序列化提供程序Serialization providers 对象可以将自己添加为自定义序列化提供程序。Objects can add themselves as custom serialization providers. 由于这些提供程序用于查找序列化程序,因此它们会长于序列化会话。Because these providers are used to locate serializers, they outlive a serialization session.

会话所有状态Session-Owned State

此状态由会话所有,在销毁会话时销毁。This state is owned by a session and is destroyed when a session is destroyed. 因此,如果序列化管理器不在活动会话中,则访问任何操作此状态的属性或方法都将引发异常。Consequently, accessing any properties or methods that would manipulate this state will throw an exception if the serialization manager is not in an active session.

ObjectObject 用法Usage
ResolveName 事件ResolveName event 序列化程序会附加 ResolveName 事件,以提供更多的名称解析。The ResolveName event is attached by a serializer to provide additional resolution of names. 当会话终止时,所有处理程序都将从此事件中分离。All handlers are detached from this event when a session terminates.
SerializationComplete 事件SerializationComplete event SerializationComplete 事件刚好在释放会话前引发。The SerializationComplete event is raised just before a session is disposed. 然后,所有处理程序都与此事件分离。Then, all handlers are detached from this event.
名称表Name table 序列化管理器维护一个表,该表在对象和其名称之间进行映射。The serialization manager maintains a table that maps between objects and their names. 序列化程序可以提供对象名以方便识别。Serializers may give objects names for easy identification. 会话终止时,将清除此名称表。This name table is cleared when the session terminates.
序列化程序缓存Serializer cache 序列化管理器维护已要求提供的序列化程序的缓存。The serialization manager maintains a cache of serializers it has been asked to supply. 当会话终止时,此缓存会被清除。This cache is cleared when the session terminates. 可以随时安全地调用公共 GetSerializer 方法,但仅当从会话内调用时,才缓存其值。The public GetSerializer method can safely be called at any time, but its value is cached only if it is called from within a session.
上下文堆栈Context stack 序列化管理器维护一个名为上下文堆栈的对象,可以使用 Context 属性访问该对象。The serialization manager maintains an object called the context stack, which you can access with the Context property. 序列化程序可以使用此堆栈来存储可供其他序列化程序使用的其他信息。Serializers can use this stack to store additional information that is available to other serializers. 例如,序列化属性值的序列化程序可以在请求要序列化的值之前推送序列化堆栈上的属性名称。For example, a serializer that is serializing a property value can push the property name on the serialization stack before asking the value to serialize. 当会话终止时,将清除此堆栈。This stack is cleared when the session is terminated.
错误列表Error list 序列化管理器维护在序列化过程中发生的错误的列表。The serialization manager maintains a list of errors that occurred during serialization. 此列表可通过 Errors 属性进行访问,在会话终止时将被清除。This list, which is accessed through the Errors property, is cleared when the session is terminated. 访问会话间的 Errors 属性将导致异常。Accessing the Errors property between sessions will result in an exception.

构造函数

DesignerSerializationManager()

初始化 DesignerSerializationManager 类的新实例。Initializes a new instance of the DesignerSerializationManager class.

DesignerSerializationManager(IServiceProvider)

使用给定的服务提供程序初始化 DesignerSerializationManager 类的新实例。Initializes a new instance of the DesignerSerializationManager class with the given service provider.

属性

Container

获取或设置该序列化管理器的容器。Gets or sets to the container for this serialization manager.

Errors

获取在序列化或反序列化期间发生的错误的列表。Gets the list of errors that occurred during serialization or deserialization.

PreserveNames

获取或设置一个值,指示 CreateInstance(Type, ICollection, String, Boolean) 方法是否应该检查容器中是否存在给定名称。Gets or sets a value indicating whether the CreateInstance(Type, ICollection, String, Boolean) method should check for the presence of the given name in the container.

PropertyProvider

获取应该用于为序列化管理器的 Properties 属性提供属性的对象。Gets the object that should be used to provide properties to the serialization manager's Properties property.

RecycleInstances

获取或设置一个值,该值指示 CreateInstance(Type, ICollection, String, Boolean) 是否始终创建类型的新实例。Gets or sets a value that indicates whether CreateInstance(Type, ICollection, String, Boolean) will always create a new instance of a type.

ValidateRecycledTypes

获取或设置一个值,该值指示 CreateInstance(Type, ICollection, String, Boolean) 方法是否验证匹配的名称表示同一类型。Gets or sets a value that indicates whether the CreateInstance(Type, ICollection, String, Boolean) method will verify that matching names refer to the same type.

方法

CreateInstance(Type, ICollection, String, Boolean)

创建类型的实例。Creates an instance of a type.

CreateSession()

创建新序列化会话。Creates a new serialization session.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetRuntimeType(String)

获取与指定类型名称对应的类型。Gets the type corresponding to the specified type name.

GetSerializer(Type, Type)

获取给定对象类型的序列化程序。Gets the serializer for the given object type.

GetService(Type)

获取请求的服务。Gets the requested service.

GetType()

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

(继承自 Object)
GetType(String)

获取请求的类型。Gets the requested type.

MemberwiseClone()

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

(继承自 Object)
OnResolveName(ResolveNameEventArgs)

引发 ResolveName 事件。Raises the ResolveName event.

OnSessionCreated(EventArgs)

引发 SessionCreated 事件。Raises the SessionCreated event.

OnSessionDisposed(EventArgs)

引发 SessionDisposed 事件。Raises the SessionDisposed event.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

事件

SessionCreated

在创建会话时发生。Occurs when a session is created.

SessionDisposed

在释放会话时发生。Occurs when a session is disposed.

显式接口实现

IDesignerSerializationManager.AddSerializationProvider(IDesignerSerializationProvider)

向序列化管理器添加自定义的序列化提供程序。Adds a custom serialization provider to the serialization manager.

IDesignerSerializationManager.Context

获取该序列化会话的上下文堆栈。Gets the context stack for this serialization session.

IDesignerSerializationManager.CreateInstance(Type, ICollection, String, Boolean)

实现 CreateInstance(Type, ICollection, String, Boolean) 方法。Implements the CreateInstance(Type, ICollection, String, Boolean) method.

IDesignerSerializationManager.GetInstance(String)

检索具有指定名称的创建对象的实例。Retrieves an instance of a created object of the specified name.

IDesignerSerializationManager.GetName(Object)

为指定的对象检索名称。Retrieves a name for the specified object.

IDesignerSerializationManager.GetSerializer(Type, Type)

为指定对象类型获取所请求类型的序列化程序。Gets a serializer of the requested type for the specified object type.

IDesignerSerializationManager.GetType(String)

获取指定名称的类型。Gets a type of the specified name.

IDesignerSerializationManager.Properties

实现 Properties 属性。Implements the Properties property.

IDesignerSerializationManager.RemoveSerializationProvider(IDesignerSerializationProvider)

移除以前添加的序列化提供程序。Removes a previously added serialization provider.

IDesignerSerializationManager.ReportError(Object)

用于报告序列化中可以恢复的错误。Used to report a recoverable error in serialization.

IDesignerSerializationManager.ResolveName

IDesignerSerializationManager.GetName(Object) 无法在序列化管理器的名称表中找到指定名称时发生。Occurs when IDesignerSerializationManager.GetName(Object) cannot locate the specified name in the serialization manager's name table.

IDesignerSerializationManager.SerializationComplete

在序列化完成时发生。Occurs when serialization is complete.

IDesignerSerializationManager.SetName(Object, String)

设置指定对象的名称。Sets the name for the specified object.

IServiceProvider.GetService(Type)

有关此成员的说明,请参见 GetService(Type) 方法。For a description of this member, see the GetService(Type) method.

适用于

另请参阅