This documentation is archived and is not being maintained.

CodeDomSerializerBase.SerializeToExpression Method

Serializes the given object into an expression.

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

protected CodeExpression SerializeToExpression(
	IDesignerSerializationManager manager,
	Object value


Type: System.ComponentModel.Design.Serialization.IDesignerSerializationManager

The IDesignerSerializationManager to use for serialization.

Type: System.Object

The object to serialize. Can be null.

Return Value

Type: System.CodeDom.CodeExpression
A CodeExpression object if value can be serialized; otherwise, null.


manager is null.

The value parameter can be null, in which case a primitive expression will be returned.

The SerializeToExpression method uses the following rules for serializing types:

  1. It first calls the GetExpression method to see if an expression has already been created for the object. If so, it returns the existing expression.

  2. It then locates the object’s serializer, and asks it to serialize.

  3. If the return value of the object’s serializer is a CodeExpression, the expression is returned.

  4. It makes one last call to GetExpression to see if the serializer added an expression.

  5. Finally, it returns null.

If no expression could be created and no suitable serializer could be found, an error will be reported through the serialization manager. No error will be reported if a serializer was found but it failed to produce an expression. In this case, it is assumed that the serializer either already reported the error or it did not attempt to serialize the object.

If the serializer returned a statement or a collection of statements, those statements will not be discarded. The SerializeToExpression method will first look for a StatementContext on the context stack and add statements to the statement context object's StatementCollection property. If there is no statement context, the SerializeToExpression method will look in the context for a CodeStatementCollection and add the statements there. If no place can be found to add the statements, an error will be generated.


You should not call the SerializeToExpression method within Serialize when serializing your own object. Instead, you should call GetExpression. If it returns null, create your own expression and call SetExpression. Then proceed with the rest of your serialization.

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0