ObjectStateFormatter ObjectStateFormatter ObjectStateFormatter ObjectStateFormatter Class


Serializes and deserializes object graphs that represent the state of an object. This class cannot be inherited.

public ref class ObjectStateFormatter sealed : System::Runtime::Serialization::IFormatter
public sealed class ObjectStateFormatter : System.Runtime.Serialization.IFormatter
type ObjectStateFormatter = class
    interface IStateFormatter
    interface IFormatter
Public NotInheritable Class ObjectStateFormatter
Implements IFormatter


The following code example demonstrates how a class that derives from the PageStatePersister class accesses the StateFormatter property to retrieve an ObjectStateFormatter instance to serialize view state and control state to a stream. This code example is part of a larger example provided for the PageStatePersister class.

// Persist any ViewState and ControlState.
public override void Save()

    if (ViewState != null || ControlState != null)
        if (Page.Session != null)
            Stream stateStream = GetSecureStream();

            StreamWriter writer = new StreamWriter(stateStream);

            IStateFormatter formatter = this.StateFormatter;
            Pair statePair = new Pair(ViewState, ControlState);

            // Serialize the statePair object to a string.
            string serializedState = formatter.Serialize(statePair);

            throw new InvalidOperationException("Session needed for StreamPageStatePersister.");
' Persist any ViewState and ControlState.
Public Overrides Sub Save()

    If Not (ViewState Is Nothing) OrElse Not (ControlState Is Nothing) Then
        If Not (Page.Session Is Nothing) Then

            Dim stateStream As Stream
            stateStream = GetSecureStream()

            ' Write a state string, using the StateFormatter.
            Dim writer As New StreamWriter(stateStream)

            Dim formatter As IStateFormatter
            formatter = Me.StateFormatter

            Dim statePair As New Pair(ViewState, ControlState)

            Dim serializedState As String
            serializedState = formatter.Serialize(statePair)

            Throw New InvalidOperationException("Session needed for StreamPageStatePersister.")
        End If
    End If
End Sub 'Save


The ObjectStateFormatter class serializes and deserializes object state graphs in a compact format.

ObjectStateFormatter is used by the PageStatePersister class and classes that derive from it to serialize view state and control state. It is also used by the LosFormatter class to provide object state graph formatting for various parts of the ASP.NET infrastructure.

The ObjectStateFormatter class is optimized to serialize and format many common .NET Framework reference types, as well as constants. The following table lists the types that are optimized.

Array DateTime Int16 String
ArrayList Double Int32 String []
Boolean Enum null (Nothing) String.Empty
Byte Hashtable Pair Triplet
Char HybridDictionary Single Type
Color IDictionary

Additionally, while conventional string types and string arrays are written to and from a serialized binary writer unaltered, some strings are optimized by creating internal string tables. Strings are optimized using these tables if the string has an associated TypeConverter object or if the string is actually an instance of the IndexedString class.

Other types not listed above are binary-serialized using a BinaryFormatter object if they implement the ISerializable interface or are decorated with the SerializableAttribute attribute. The ObjectStateFormatter class is not optimized for any of these serializable types.

If the ObjectStateFormatter class encounters a type that is not serializable, an ArgumentException exception is thrown.


ObjectStateFormatter() ObjectStateFormatter() ObjectStateFormatter() ObjectStateFormatter()

Initializes a new instance of the ObjectStateFormatter class.


Deserialize(Stream) Deserialize(Stream) Deserialize(Stream) Deserialize(Stream)

Deserializes an object state graph from its binary-serialized form that is contained in the specified Stream object.

Deserialize(String) Deserialize(String) Deserialize(String) Deserialize(String)

Deserializes an object state graph from its serialized base64-encoded string form.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Serialize(Object) Serialize(Object) Serialize(Object) Serialize(Object)

Serializes an object state graph to a base64-encoded string.

Serialize(Stream, Object) Serialize(Stream, Object) Serialize(Stream, Object) Serialize(Stream, Object)

Serializes an object state graph to the specified Stream object.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

IFormatter.Binder IFormatter.Binder IFormatter.Binder IFormatter.Binder

For a description of this member, see Binder.

IFormatter.Context IFormatter.Context IFormatter.Context IFormatter.Context

For a description of this member, see Context.

IFormatter.Deserialize(Stream) IFormatter.Deserialize(Stream) IFormatter.Deserialize(Stream) IFormatter.Deserialize(Stream)

For a description of this member, see Deserialize(Stream).

IFormatter.Serialize(Stream, Object) IFormatter.Serialize(Stream, Object) IFormatter.Serialize(Stream, Object) IFormatter.Serialize(Stream, Object)

For a description of this member, see Serialize(Stream, Object).

IFormatter.SurrogateSelector IFormatter.SurrogateSelector IFormatter.SurrogateSelector IFormatter.SurrogateSelector

For a description of this member, see SurrogateSelector.

IStateFormatter.Deserialize(String) IStateFormatter.Deserialize(String) IStateFormatter.Deserialize(String) IStateFormatter.Deserialize(String)

For a description of this member, see Deserialize(String).

IStateFormatter.Serialize(Object) IStateFormatter.Serialize(Object) IStateFormatter.Serialize(Object) IStateFormatter.Serialize(Object)

For a description of this member, see Serialize(Object).

Applies to