This documentation is archived and is not being maintained.

ObjectStateFormatter Class

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


Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

public sealed class ObjectStateFormatter : IStateFormatter, 

The ObjectStateFormatter type exposes the following members.

Public methodObjectStateFormatterInitializes a new instance of the ObjectStateFormatter class.

Public methodDeserialize(Stream)Deserializes an object state graph from its binary-serialized form that is contained in the specified Stream object.
Public methodDeserialize(String)Deserializes an object state graph from its serialized base64-encoded string form.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSerialize(Object)Serializes an object state graph to a base64-encoded string.
Public methodSerialize(Stream, Object)Serializes an object state graph to the specified Stream object.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Explicit interface implemetationPrivate propertyIFormatter.BinderFor a description of this member, see Formatter.Binder.
Explicit interface implemetationPrivate propertyIFormatter.ContextFor a description of this member, see IFormatter.Context.
Explicit interface implemetationPrivate methodIFormatter.DeserializeInfrastructure. For a description of this member, see ObjectStateFormatter.Deserialize.
Explicit interface implemetationPrivate methodIFormatter.SerializeInfrastructure. For a description of this member, see ObjectStateFormatter.Serialize.
Explicit interface implemetationPrivate propertyIFormatter.SurrogateSelectorFor a description of this member, see System.Runtime.Serialization.SurrogateSelector.
Explicit interface implemetationPrivate methodIStateFormatter.DeserializeFor a description of this member, see ObjectStateFormatter.Deserialize.
Explicit interface implemetationPrivate methodIStateFormatter.SerializeFor a description of this member, see ObjectStateFormatter.Serialize.

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.

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.

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.");

.NET Framework

Supported in: 4, 3.5, 3.0, 2.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.