CodeDomSerializer 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将对象图序列化为一系列 CodeDOM 语句。 此类提供序列化程序的抽象基类。
public ref class CodeDomSerializer abstractpublic ref class CodeDomSerializer : System::ComponentModel::Design::Serialization::CodeDomSerializerBasepublic abstract class CodeDomSerializerpublic class CodeDomSerializer : System.ComponentModel.Design.Serialization.CodeDomSerializerBasetype CodeDomSerializer = classtype CodeDomSerializer = class
    inherit CodeDomSerializerBasePublic MustInherit Class CodeDomSerializerPublic Class CodeDomSerializer
Inherits CodeDomSerializerBase- 继承
- 
				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 ,必须:
- 定义一个从 CodeDomSerializer 派生的类。 
- 实现序列化或反序列化方法的方法重写。 (有关详细信息,请参阅以下信息。) 
- 使用 DesignerSerializerAttribute将自定义CodeDomSerializer实现与某个类型的组件相关联。 
若要实现用于为组件生成配置代码的序列化方法,请执行以下操作:
- 在派生自 CodeDomSerializer的类中,重写基类的相应序列化或反序列化方法。 
- 如果希望默认序列化程序生成执行默认组件配置的代码语句,则必须获取并调用组件的基本序列化程序。 若要获取组件的基本序列化程序,请调用 GetSerializer 传递给方法重写的 IDesignerSerializationManager 的 方法。 向 GetSerializer 方法传递要序列化其配置的组件类型,以及正在请求的序列化程序基类型,即 CodeDomSerializer。 使用传递给方法重写的 和 对象,调用 IDesignerSerializationManager 在基本序列化程序上重写的同名方法。 如果要实现 Serialize 方法, Serialize 则基本序列化程序的 方法将返回 一个 对象。 此对象的类型取决于基本序列化程序的类型,该类型取决于要序列化其值的组件类型。 如果要实现 SerializeEvents、 SerializeProperties或 SerializePropertiesToResources 方法,则必须创建一个新的 CodeStatementCollection 以包含生成的代码语句,并将其传递给 方法。 
- 如果已调用基序列化程序方法,则具有一个 CodeStatementCollection ,其中包含要生成的用于初始化组件的语句。 否则,应创建 。CodeStatementCollection 可以将表示在组件配置代码中生成的语句的对象添加到 CodeStatement 此集合。 
- 返回表示 CodeStatementCollection 要生成的源代码以配置组件。 
实施者说明
当从 CodeDomSerializer 继承时,必须重写下面的成员:Deserialize(IDesignerSerializationManager, Object) 和 Serialize(IDesignerSerializationManager, Object)。
构造函数
| CodeDomSerializer() | 初始化 CodeDomSerializer 类的新实例。 |