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

The CodeDomSerializer type exposes the following members.

  Name Description
Public method CodeDomSerializer Initializes a new instance of the CodeDomSerializer class.
Top
  Name Description
Public method Deserialize Deserializes the specified serialized CodeDOM object into an object.
Protected method DeserializeExpression Deserializes the given expression into an in-memory object. (Inherited from CodeDomSerializerBase.)
Protected method DeserializeInstance Returns an instance of the given type. (Inherited from CodeDomSerializerBase.)
Protected method DeserializePropertiesFromResources Deserializes properties on the given object from the invariant culture’s resource bundle. (Inherited from CodeDomSerializerBase.)
Protected method DeserializeStatement Deserializes a statement by interpreting and executing a CodeDOM statement. (Inherited from CodeDomSerializerBase.)
Protected method DeserializeStatementToInstance Deserializes a single statement.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Protected method GetExpression Returns an expression for the given object. (Inherited from CodeDomSerializerBase.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Protected method GetSerializer(IDesignerSerializationManager, Object) Locates a serializer for the given object value. (Inherited from CodeDomSerializerBase.)
Protected method GetSerializer(IDesignerSerializationManager, Type) Locates a serializer for the given type. (Inherited from CodeDomSerializerBase.)
Public method GetTargetComponentName Determines which statement group the given statement should belong to.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method GetUniqueName Returns a unique name for the given object. (Inherited from CodeDomSerializerBase.)
Protected method IsSerialized(IDesignerSerializationManager, Object) Returns a value indicating whether the given object has been serialized. (Inherited from CodeDomSerializerBase.)
Protected method IsSerialized(IDesignerSerializationManager, Object, Boolean) Returns a value indicating whether the given object has been serialized, optionally considering preset expressions. (Inherited from CodeDomSerializerBase.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Serialize Serializes the specified object into a CodeDOM object.
Public method SerializeAbsolute Serializes the given object, accounting for default values.
Protected method SerializeCreationExpression Returns an expression representing the creation of the given object. (Inherited from CodeDomSerializerBase.)
Protected method SerializeEvent Serializes the given event into the given statement collection. (Inherited from CodeDomSerializerBase.)
Protected method SerializeEvents Serializes the specified events into the given statement collection. (Inherited from CodeDomSerializerBase.)
Public method SerializeMember Serializes the given member on the given object.
Public method SerializeMemberAbsolute Serializes the given member, accounting for default values.
Protected method SerializeProperties Serializes the properties on the given object into the given statement collection. (Inherited from CodeDomSerializerBase.)
Protected method SerializePropertiesToResources Serializes the properties on the given object into the invariant culture’s resource bundle. (Inherited from CodeDomSerializerBase.)
Protected method SerializeProperty Serializes a property on the given object. (Inherited from CodeDomSerializerBase.)
Protected method SerializeResource Serializes the given object into a resource bundle using the given resource name. (Inherited from CodeDomSerializerBase.)
Protected method SerializeResourceInvariant Serializes the given object into a resource bundle using the given resource name. (Inherited from CodeDomSerializerBase.)
Protected method SerializeToExpression Serializes the given object into an expression. (Inherited from CodeDomSerializerBase.)
Protected method SerializeToReferenceExpression Obsolete. Serializes the specified value to a CodeDOM expression.
Protected method SerializeToResourceExpression(IDesignerSerializationManager, Object) Serializes the given object into an expression. (Inherited from CodeDomSerializerBase.)
Protected method SerializeToResourceExpression(IDesignerSerializationManager, Object, Boolean) Serializes the given object into an expression appropriate for the invariant culture. (Inherited from CodeDomSerializerBase.)
Protected method SetExpression(IDesignerSerializationManager, Object, CodeExpression) Associates an object with an expression. (Inherited from CodeDomSerializerBase.)
Protected method SetExpression(IDesignerSerializationManager, Object, CodeExpression, Boolean) Associates an object with an expression, optionally specifying a preset expression. (Inherited from CodeDomSerializerBase.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top

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

}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
CodeDomSerializer for Compact Framework
As I know CodeDomeSerializer is not supported for Compact Framework by default. Are there any workaround for this problem?