Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

CodeDomSerializer Class

Serializes an object graph to a series of CodeDOM statements. This class provides an abstract base class for a serializer.

Namespace: System.ComponentModel.Design.Serialization
Assembly: System.Design (in system.design.dll)

public class CodeDomSerializer : CodeDomSerializerBase
public class CodeDomSerializer extends CodeDomSerializerBase
public class CodeDomSerializer extends CodeDomSerializerBase
Not applicable.

You can implement a custom CodeDomSerializer to control the generation of component initialization code for a type of component at design time.

To implement a custom CodeDomSerializer for a type, you must:

  1. Define a class that derives from CodeDomSerializer.

  2. Implement method overrides for serialization or deserialization methods. (See the information below for details.)

  3. Associate your custom CodeDomSerializer implementation with a type of component using a DesignerSerializerAttribute.

To implement a serialization method for generating configuration code for a component:

  1. Within a class that derives from CodeDomSerializer, override an appropriate serialization or deserialization method of the base class.

  2. If you want the default serializer to generate code statements that perform the default component configuration, you must obtain and call the base serializer for the component. To obtain the base serializer for the component, call the GetSerializer method of the IDesignerSerializationManager passed to your method override. Pass the GetSerializer method the type of the component to serialize the configuration of, along with the base type of serializer you are requesting, which is CodeDomSerializer. Call the method of the same name you are overriding on the base serializer, using the IDesignerSerializationManager and object passed to your method override. If you are implementing the Serialize method, the Serialize method of the base serializer will return an object. The type of this object depends on the type of base serializer which depends on the type of component you are serializing the values of. If you are implementing the SerializeEvents, SerializeProperties, or SerializePropertiesToResources method, you must create a new CodeStatementCollection to contain the generated code statements, and pass it to the method.

  3. If you have called a base serializer method, you will have a CodeStatementCollection that contains the statements to generate to initialize the component. Otherwise you should create a CodeStatementCollection. You can add CodeStatement objects representing statements to generate in the component configuration code to this collection.

  4. Return the CodeStatementCollection that represents the source code to generate to configure the component.

Notes to Inheritors: When you inherit from CodeDomSerializer, you must override the following members: Deserialize and Serialize.

The following code example illustrates how to create a custom CodeDOM serializer that derives from CodeDomSerializer.

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

}

import System.*;
import System.CodeDom.*;
import System.ComponentModel.*;
import System.ComponentModel.Design.*;
import System.ComponentModel.Design.Serialization.*;
import System.Drawing.*;
import System.Windows.Forms.*;

   
class MyCodeDomSerializer extends CodeDomSerializer
{
    public Object Deserialize(IDesignerSerializationManager manager,
        Object codeObject) {

        // This is how we associate the component with the serializer.
        CodeDomSerializer baseClassSerializer = (CodeDomSerializer)
            manager.GetSerializer(MyComponent.class.ToType().get_BaseType(),
            CodeDomSerializer.class.ToType());
        
        /* This is the simplest case, in which the class just calls the base
            class to do the work. 
         */
         return baseClassSerializer.Deserialize(manager, codeObject);
    } //Deserialize
      
    public Object Serialize(IDesignerSerializationManager manager,
        Object value)
    {
        /* Associate the component with the serializer in the same manner as 
            with Deserialize 
         */

        CodeDomSerializer baseClassSerializer = (CodeDomSerializer)
            manager.GetSerializer(MyComponent.class.ToType().
            get_BaseType(), CodeDomSerializer.class.ToType());
        Object codeObject = baseClassSerializer.Serialize(manager, value);
         
        /* Anything could be in the codeObject.  This sample operates on a
                CodeStatementCollection. 
         */

        if (codeObject instanceof 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;
    } //Serialize
} //MyCodeDomSerializer
      
/** @attribute DesignerSerializer(MyCodeDomSerializer.class,
  CodeDomSerializer.class)
 */

public class MyComponent extends Component
{
    private String localProperty = "Component Property Value";
      
    /** @property 
     */
    public String get_LocalProperty()
    {
        return localProperty;
    } //get_LocalProperty

    /** @property 
     */
    public void set_LocalProperty (String value )
    {
        localProperty = value;
    } //set_LocalProperty
} //MyComponent

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

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.