CodeDomSerializer 类

定义

将对象图序列化为一系列 CodeDOM 语句。 此类提供序列化程序的抽象基类。

public ref class CodeDomSerializer abstract
public ref class CodeDomSerializer : System::ComponentModel::Design::Serialization::CodeDomSerializerBase
public abstract class CodeDomSerializer
public class CodeDomSerializer : System.ComponentModel.Design.Serialization.CodeDomSerializerBase
type CodeDomSerializer = class
type CodeDomSerializer = class
    inherit CodeDomSerializerBase
Public MustInherit Class CodeDomSerializer
Public Class CodeDomSerializer
Inherits CodeDomSerializerBase
继承
CodeDomSerializer
继承
CodeDomSerializer
派生

示例

下面的代码示例演示如何创建派生自 CodeDomSerializer的自定义 CodeDOM 序列化程序。

#using <System.Drawing.dll>
#using <System.dll>
#using <System.Design.dll>

using namespace System;
using namespace System::CodeDom;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::ComponentModel::Design::Serialization;
using namespace System::Drawing;
using namespace System::Windows::Forms;

namespace CodeDomSerializerSample
{
   ref class MyComponent;
   private ref class MyCodeDomSerializer: public CodeDomSerializer
   {
   public:
      Object^ Deserialize( IDesignerSerializationManager^ manager, Object^ codeObject ) new
      {
         // This is how we associate the component with the serializer.
         CodeDomSerializer^ baseClassSerializer = (CodeDomSerializer^)(
            manager->GetSerializer(
               MyComponent::typeid->BaseType, CodeDomSerializer::typeid ));
         
         /* This is the simplest case, in which the class just calls the base class
            to do the work. */
         return baseClassSerializer->Deserialize( manager, codeObject );
      }

      Object^ Serialize( IDesignerSerializationManager^ manager, Object^ value ) new
      {
         /* Associate the component with the serializer in the same manner as with
            Deserialize */
         CodeDomSerializer^ baseClassSerializer = (CodeDomSerializer^)(
            manager->GetSerializer(
               MyComponent::typeid->BaseType, CodeDomSerializer::typeid ));

         Object^ codeObject = baseClassSerializer->Serialize( manager, value );
         
         /* Anything could be in the codeObject.  This sample operates on a
            CodeStatementCollection. */
         if ( (CodeStatementCollection^)(codeObject) )
         {
            CodeStatementCollection^ statements = (CodeStatementCollection^)(codeObject);
            
            // The code statement collection is valid, so add a comment.
            String^ commentText = "This comment was added to this object by a custom serializer.";
            CodeCommentStatement^ comment = gcnew CodeCommentStatement( commentText );
            statements->Insert( 0, comment );
         }
         return codeObject;
      }
   };

   [DesignerSerializer(CodeDomSerializerSample::MyCodeDomSerializer::typeid,
      CodeDomSerializer::typeid)]
   public ref class MyComponent: public Component
   {
   private:
      String^ localProperty;

   public:
      MyComponent()
      {
         localProperty = "Component Property Value";
      }

      property String^ LocalProperty 
      {
         String^ get()
         {
            return localProperty;
         }
         void set( String^ value )
         {
            localProperty = value;
         }
      }
   };
}
using System;
using System.CodeDom;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Windows.Forms;
 
namespace CodeDomSerializerSample
{
    internal class MyCodeDomSerializer : CodeDomSerializer {
        public override object Deserialize(IDesignerSerializationManager manager, object codeObject) {
            // This is how we associate the component with the serializer.
                CodeDomSerializer baseClassSerializer = (CodeDomSerializer)manager.
                GetSerializer(typeof(MyComponent).BaseType, typeof(CodeDomSerializer));

            /* This is the simplest case, in which the class just calls the base class
                to do the work. */
            return baseClassSerializer.Deserialize(manager, codeObject);
        }
 
        public override object Serialize(IDesignerSerializationManager manager, object value) {
            /* Associate the component with the serializer in the same manner as with
                Deserialize */
            CodeDomSerializer baseClassSerializer = (CodeDomSerializer)manager.
                GetSerializer(typeof(MyComponent).BaseType, typeof(CodeDomSerializer));
 
            object codeObject = baseClassSerializer.Serialize(manager, value);
 
            /* Anything could be in the codeObject.  This sample operates on a
                CodeStatementCollection. */
            if (codeObject is CodeStatementCollection) {
                CodeStatementCollection statements = (CodeStatementCollection)codeObject;
 
                // The code statement collection is valid, so add a comment.
                string commentText = "This comment was added to this object by a custom serializer.";
                CodeCommentStatement comment = new CodeCommentStatement(commentText);
                statements.Insert(0, comment);
            }
            return codeObject;
        }
    }
 
    [DesignerSerializer(typeof(MyCodeDomSerializer), typeof(CodeDomSerializer))]
    public class MyComponent : Component {
        private string localProperty = "Component Property Value";
        public string LocalProperty {
            get {
                return localProperty;
            }
            set {
                localProperty = value;
            }
        }
    }
}
Imports System.CodeDom
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.ComponentModel.Design.Serialization
Imports System.Drawing
Imports System.Windows.Forms

Namespace CodeDomSerializerSample
   Friend Class MyCodeDomSerializer
      Inherits CodeDomSerializer

      Public Overrides Function Deserialize(ByVal manager As IDesignerSerializationManager, _
                                                ByVal codeObject As Object) As Object
         ' This is how we associate the component with the serializer.
         Dim baseClassSerializer As CodeDomSerializer = CType(manager.GetSerializer( _
                GetType(MyComponent).BaseType, GetType(CodeDomSerializer)), CodeDomSerializer)

         ' This is the simplest case, in which the class just calls the base class
         '  to do the work. 
         Return baseClassSerializer.Deserialize(manager, codeObject)
      End Function 'Deserialize

      Public Overrides Function Serialize(ByVal manager As IDesignerSerializationManager, _
                                            ByVal value As Object) As Object
         ' Associate the component with the serializer in the same manner as with
         '  Deserialize
         Dim baseClassSerializer As CodeDomSerializer = CType(manager.GetSerializer( _
                GetType(MyComponent).BaseType, GetType(CodeDomSerializer)), CodeDomSerializer)

         Dim codeObject As Object = baseClassSerializer.Serialize(manager, value)

         ' Anything could be in the codeObject.  This sample operates on a
         '  CodeStatementCollection.
         If TypeOf codeObject Is CodeStatementCollection Then
            Dim statements As CodeStatementCollection = CType(codeObject, CodeStatementCollection)

            ' The code statement collection is valid, so add a comment.
            Dim commentText As String = "This comment was added to this object by a custom serializer."
            Dim comment As New CodeCommentStatement(commentText)
            statements.Insert(0, comment)
         End If
         Return codeObject
      End Function 'Serialize
   End Class

   <DesignerSerializer(GetType(MyCodeDomSerializer), GetType(CodeDomSerializer))> _
   Public Class MyComponent
      Inherits Component
      Private localProperty As String = "Component Property Value"

      Public Property LocalProp() As String
         Get
            Return localProperty
         End Get
         Set(ByVal Value As String)
            localProperty = Value
         End Set
      End Property
   End Class

End Namespace

注解

可以实现自定义 CodeDomSerializer ,以控制在设计时为某一类型的组件生成组件初始化代码。

若要为类型实现自定义 CodeDomSerializer ,必须:

  1. 定义一个从 CodeDomSerializer 派生的类。

  2. 实现序列化或反序列化方法的方法重写。 (有关详细信息,请参阅以下信息。)

  3. 使用 DesignerSerializerAttribute将自定义CodeDomSerializer实现与某个类型的组件相关联。

若要实现用于为组件生成配置代码的序列化方法,请执行以下操作:

  1. 在派生自 CodeDomSerializer的类中,重写基类的相应序列化或反序列化方法。

  2. 如果希望默认序列化程序生成执行默认组件配置的代码语句,则必须获取并调用组件的基本序列化程序。 若要获取组件的基本序列化程序,请调用 GetSerializer 传递给方法重写的 IDesignerSerializationManager 的 方法。 向 GetSerializer 方法传递要序列化其配置的组件类型,以及正在请求的序列化程序基类型,即 CodeDomSerializer。 使用传递给方法重写的 和 对象,调用 IDesignerSerializationManager 在基本序列化程序上重写的同名方法。 如果要实现 Serialize 方法, Serialize 则基本序列化程序的 方法将返回 一个 对象。 此对象的类型取决于基本序列化程序的类型,该类型取决于要序列化其值的组件类型。 如果要实现 SerializeEventsSerializePropertiesSerializePropertiesToResources 方法,则必须创建一个新的 CodeStatementCollection 以包含生成的代码语句,并将其传递给 方法。

  3. 如果已调用基序列化程序方法,则具有一个 CodeStatementCollection ,其中包含要生成的用于初始化组件的语句。 否则,应创建 。CodeStatementCollection 可以将表示在组件配置代码中生成的语句的对象添加到 CodeStatement 此集合。

  4. 返回表示 CodeStatementCollection 要生成的源代码以配置组件。

实施者说明

当从 CodeDomSerializer 继承时,必须重写下面的成员:Deserialize(IDesignerSerializationManager, Object)Serialize(IDesignerSerializationManager, Object)

构造函数

CodeDomSerializer()

初始化 CodeDomSerializer 类的新实例。

方法

Deserialize(IDesignerSerializationManager, Object)

将指定的已序列化的 CodeDOM 对象反序列化为一个对象。

DeserializeExpression(IDesignerSerializationManager, String, CodeExpression)

对指定的表达式进行反序列化。

DeserializeExpression(IDesignerSerializationManager, String, CodeExpression)

将给定表达式反序列化为内存中的对象。

(继承自 CodeDomSerializerBase)
DeserializeInstance(IDesignerSerializationManager, Type, Object[], String, Boolean)

返回给定类型的实例。

(继承自 CodeDomSerializerBase)
DeserializePropertiesFromResources(IDesignerSerializationManager, Object, Attribute[])

如果指定了筛选器,则对与指定筛选器匹配的指定对象的属性进行反序列化。

DeserializePropertiesFromResources(IDesignerSerializationManager, Object, Attribute[])

在给定对象上将属性从固定区域性资源包反序列化。

(继承自 CodeDomSerializerBase)
DeserializeStatement(IDesignerSerializationManager, CodeStatement)

对指定语句进行反序列化。

DeserializeStatement(IDesignerSerializationManager, CodeStatement)

通过解释并执行 CodeDOM 语句反序列化语句。

(继承自 CodeDomSerializerBase)
DeserializeStatementToInstance(IDesignerSerializationManager, CodeStatement)

反序列化单条语句。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetExpression(IDesignerSerializationManager, Object)

返回给定对象的表达式。

(继承自 CodeDomSerializerBase)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetSerializer(IDesignerSerializationManager, Object)

定位给定对象值的序列化程序。

(继承自 CodeDomSerializerBase)
GetSerializer(IDesignerSerializationManager, Type)

定位给定类型的序列化程序。

(继承自 CodeDomSerializerBase)
GetTargetComponentName(CodeStatement, CodeExpression, Type)

确定给定的语句应属于的语句组。

GetType()

获取当前实例的 Type

(继承自 Object)
GetUniqueName(IDesignerSerializationManager, Object)

返回给定对象的唯一名称。

(继承自 CodeDomSerializerBase)
IsSerialized(IDesignerSerializationManager, Object)

返回一个值,指示给定对象是否已序列化。

(继承自 CodeDomSerializerBase)
IsSerialized(IDesignerSerializationManager, Object, Boolean)

返回一个值,指示给定对象是否已序列化,也可以考虑预设表达式。

(继承自 CodeDomSerializerBase)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Serialize(IDesignerSerializationManager, Object)

将指定的对象序列化为 CodeDOM 对象。

SerializeAbsolute(IDesignerSerializationManager, Object)

序列化给定的对象(考虑默认值)。

SerializeCreationExpression(IDesignerSerializationManager, Object, Boolean)

返回一个表示给定对象的创建的表达式。

(继承自 CodeDomSerializerBase)
SerializeEvent(IDesignerSerializationManager, CodeStatementCollection, Object, EventDescriptor)

将给定事件序列化为给定语句集合。

(继承自 CodeDomSerializerBase)
SerializeEvents(IDesignerSerializationManager, CodeStatementCollection, Object, Attribute[])

对指定对象的所有事件进行序列化。

SerializeEvents(IDesignerSerializationManager, CodeStatementCollection, Object, Attribute[])

将指定的事件序列化为给定语句集合。

(继承自 CodeDomSerializerBase)
SerializeMember(IDesignerSerializationManager, Object, MemberDescriptor)

在给定对象上序列化给定成员。

SerializeMemberAbsolute(IDesignerSerializationManager, Object, MemberDescriptor)

考虑到默认值的情况下,序列化给定成员。

SerializeProperties(IDesignerSerializationManager, CodeStatementCollection, Object, Attribute[])

使用指定的筛选器,对指定对象的所有属性进行序列化。

SerializeProperties(IDesignerSerializationManager, CodeStatementCollection, Object, Attribute[])

将给定对象上的属性序列化为给定语句集合。

(继承自 CodeDomSerializerBase)
SerializePropertiesToResources(IDesignerSerializationManager, CodeStatementCollection, Object, Attribute[])

将指定的属性序列化为资源。

SerializePropertiesToResources(IDesignerSerializationManager, CodeStatementCollection, Object, Attribute[])

在给定对象上将属性序列化为固定区域性的资源包。

(继承自 CodeDomSerializerBase)
SerializeProperty(IDesignerSerializationManager, CodeStatementCollection, Object, PropertyDescriptor)

序列化给定对象上的属性。

(继承自 CodeDomSerializerBase)
SerializeResource(IDesignerSerializationManager, String, Object)

将给定对象序列化为使用给定资源名称的资源包。

SerializeResource(IDesignerSerializationManager, String, Object)

将给定对象序列化为使用给定资源名称的资源包。

(继承自 CodeDomSerializerBase)
SerializeResourceInvariant(IDesignerSerializationManager, String, Object)

使用固定区域性将给定对象序列化为使用给定资源名称的资源包。

SerializeResourceInvariant(IDesignerSerializationManager, String, Object)

将给定对象序列化为使用给定资源名称的资源包。

(继承自 CodeDomSerializerBase)
SerializeToExpression(IDesignerSerializationManager, Object)

将指定值序列化为 CodeDOM 表达式。

SerializeToExpression(IDesignerSerializationManager, Object)

将给定对象序列化为表达式。

(继承自 CodeDomSerializerBase)
SerializeToReferenceExpression(IDesignerSerializationManager, Object)
已过时.
已过时.

将指定值序列化为 CodeDOM 表达式。

SerializeToResourceExpression(IDesignerSerializationManager, Object)

将给定对象序列化为表达式。

(继承自 CodeDomSerializerBase)
SerializeToResourceExpression(IDesignerSerializationManager, Object, Boolean)

将给定对象序列化为适合于固定区域性的表达式。

(继承自 CodeDomSerializerBase)
SetExpression(IDesignerSerializationManager, Object, CodeExpression)

将一个对象与表达式相关联。

(继承自 CodeDomSerializerBase)
SetExpression(IDesignerSerializationManager, Object, CodeExpression, Boolean)

将对象与表达式关联,并选择指定预设表达式。

(继承自 CodeDomSerializerBase)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅