ObjectStateFormatter Class
TOC
Collapse the table of content
Expand the table of content

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)

System.Object
  System.Web.UI.ObjectStateFormatter

public sealed class ObjectStateFormatter : IStateFormatter, IFormatter

NameDescription
System_CAPS_pubmethodObjectStateFormatter()

Initializes a new instance of the ObjectStateFormatter class.

NameDescription
System_CAPS_pubmethodDeserialize(Stream)

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

System_CAPS_pubmethodDeserialize(String)

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

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodSerialize(Object)

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

System_CAPS_pubmethodSerialize(Stream, Object)

Serializes an object state graph to the specified Stream object.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodIFormatter.Deserialize(Stream)

This API supports the product infrastructure and is not intended to be used directly from your code. For a description of this member, see ObjectStateFormatter.Deserialize.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIFormatter.Serialize(Stream, Object)

This API supports the product infrastructure and is not intended to be used directly from your code. For a description of this member, see ObjectStateFormatter.Serialize.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIStateFormatter.Deserialize(String)

For a description of this member, see ObjectStateFormatter.Deserialize.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIStateFormatter.Serialize(Object)

For a description of this member, see ObjectStateFormatter.Serialize.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIFormatter.Binder

For a description of this member, see Formatter.Binder.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIFormatter.Context

For a description of this member, see IFormatter.Context.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIFormatter.SurrogateSelector

For a description of this member, see System.Runtime.Serialization.SurrogateSelector.

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.

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

            writer.Write(serializedState);
            writer.Close();
            stateStream.Close();
        }
        else
            throw new InvalidOperationException("Session needed for StreamPageStatePersister.");
    }
}

.NET Framework
Available since 2.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft