Edit

Share via


CodeDomSerializer Class

Definition

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

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
Inheritance
CodeDomSerializer
Inheritance
CodeDomSerializer
Derived

Examples

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

#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

Remarks

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 Implementers

When you inherit from CodeDomSerializer, you must override the following members: Deserialize(IDesignerSerializationManager, Object) and Serialize(IDesignerSerializationManager, Object).

Constructors

CodeDomSerializer()

Initializes a new instance of the CodeDomSerializer class.

Methods

Deserialize(IDesignerSerializationManager, Object)

Deserializes the specified serialized CodeDOM object into an object.

DeserializeExpression(IDesignerSerializationManager, String, CodeExpression)

Deserializes the specified expression.

DeserializeExpression(IDesignerSerializationManager, String, CodeExpression)

Deserializes the given expression into an in-memory object.

(Inherited from CodeDomSerializerBase)
DeserializeInstance(IDesignerSerializationManager, Type, Object[], String, Boolean)

Returns an instance of the given type.

(Inherited from CodeDomSerializerBase)
DeserializePropertiesFromResources(IDesignerSerializationManager, Object, Attribute[])

Deserializes the properties of the specified object that match the specified filter, if a filter was specified.

DeserializePropertiesFromResources(IDesignerSerializationManager, Object, Attribute[])

Deserializes properties on the given object from the invariant culture's resource bundle.

(Inherited from CodeDomSerializerBase)
DeserializeStatement(IDesignerSerializationManager, CodeStatement)

Deserializes the specified statement.

DeserializeStatement(IDesignerSerializationManager, CodeStatement)

Deserializes a statement by interpreting and executing a CodeDOM statement.

(Inherited from CodeDomSerializerBase)
DeserializeStatementToInstance(IDesignerSerializationManager, CodeStatement)

Deserializes a single statement.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetExpression(IDesignerSerializationManager, Object)

Returns an expression for the given object.

(Inherited from CodeDomSerializerBase)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetSerializer(IDesignerSerializationManager, Object)

Locates a serializer for the given object value.

(Inherited from CodeDomSerializerBase)
GetSerializer(IDesignerSerializationManager, Type)

Locates a serializer for the given type.

(Inherited from CodeDomSerializerBase)
GetTargetComponentName(CodeStatement, CodeExpression, Type)

Determines which statement group the given statement should belong to.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
GetUniqueName(IDesignerSerializationManager, Object)

Returns a unique name for the given object.

(Inherited from CodeDomSerializerBase)
IsSerialized(IDesignerSerializationManager, Object, Boolean)

Returns a value indicating whether the given object has been serialized, optionally considering preset expressions.

(Inherited from CodeDomSerializerBase)
IsSerialized(IDesignerSerializationManager, Object)

Returns a value indicating whether the given object has been serialized.

(Inherited from CodeDomSerializerBase)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Serialize(IDesignerSerializationManager, Object)

Serializes the specified object into a CodeDOM object.

SerializeAbsolute(IDesignerSerializationManager, Object)

Serializes the given object, accounting for default values.

SerializeCreationExpression(IDesignerSerializationManager, Object, Boolean)

Returns an expression representing the creation of the given object.

(Inherited from CodeDomSerializerBase)
SerializeEvent(IDesignerSerializationManager, CodeStatementCollection, Object, EventDescriptor)

Serializes the given event into the given statement collection.

(Inherited from CodeDomSerializerBase)
SerializeEvents(IDesignerSerializationManager, CodeStatementCollection, Object, Attribute[])

Serializes all events of the specified object.

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

Serializes the specified events into the given statement collection.

(Inherited from CodeDomSerializerBase)
SerializeMember(IDesignerSerializationManager, Object, MemberDescriptor)

Serializes the given member on the given object.

SerializeMemberAbsolute(IDesignerSerializationManager, Object, MemberDescriptor)

Serializes the given member, accounting for default values.

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

Serializes all properties for the specified object, using the specified filter.

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

Serializes the properties on the given object into the given statement collection.

(Inherited from CodeDomSerializerBase)
SerializePropertiesToResources(IDesignerSerializationManager, CodeStatementCollection, Object, Attribute[])

Serializes the specified properties to resources.

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

Serializes the properties on the given object into the invariant culture's resource bundle.

(Inherited from CodeDomSerializerBase)
SerializeProperty(IDesignerSerializationManager, CodeStatementCollection, Object, PropertyDescriptor)

Serializes a property on the given object.

(Inherited from CodeDomSerializerBase)
SerializeResource(IDesignerSerializationManager, String, Object)

Serializes the given object into a resource bundle using the given resource name.

SerializeResource(IDesignerSerializationManager, String, Object)

Serializes the given object into a resource bundle using the given resource name.

(Inherited from CodeDomSerializerBase)
SerializeResourceInvariant(IDesignerSerializationManager, String, Object)

Serializes the given object into a resource bundle using the given resource name using invariant culture.

SerializeResourceInvariant(IDesignerSerializationManager, String, Object)

Serializes the given object into a resource bundle using the given resource name.

(Inherited from CodeDomSerializerBase)
SerializeToExpression(IDesignerSerializationManager, Object)

Serializes the specified value to a CodeDOM expression.

SerializeToExpression(IDesignerSerializationManager, Object)

Serializes the given object into an expression.

(Inherited from CodeDomSerializerBase)
SerializeToReferenceExpression(IDesignerSerializationManager, Object)
Obsolete.
Obsolete.

Serializes the specified value to a CodeDOM expression.

SerializeToResourceExpression(IDesignerSerializationManager, Object, Boolean)

Serializes the given object into an expression appropriate for the invariant culture.

(Inherited from CodeDomSerializerBase)
SerializeToResourceExpression(IDesignerSerializationManager, Object)

Serializes the given object into an expression.

(Inherited from CodeDomSerializerBase)
SetExpression(IDesignerSerializationManager, Object, CodeExpression, Boolean)

Associates an object with an expression, optionally specifying a preset expression.

(Inherited from CodeDomSerializerBase)
SetExpression(IDesignerSerializationManager, Object, CodeExpression)

Associates an object with an expression.

(Inherited from CodeDomSerializerBase)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also