ObjectStateFormatter.Serialize Method

Definition

Serializes an object state graph.

Overloads

Serialize(Object)

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

Serialize(Stream, Object)

Serializes an object state graph to the specified Stream object.

Serialize(Object)

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

public:
 System::String ^ Serialize(System::Object ^ stateGraph);
public string Serialize (object stateGraph);
member this.Serialize : obj -> string
Public Function Serialize (stateGraph As Object) As String

Parameters

stateGraph
Object

The object to serialize.

Returns

A base-64 encoded string that represents the serialized object state of the stateGraph parameter.

Examples

The following code example demonstrates how to serialize the values of a set of control properties to a base64-encoded string using the Serialize(Object) method. The string can be deserialized at a later time with the Deserialize(String) method.


ArrayList controlProperties = new ArrayList(3);

controlProperties.Add( SortDirection );
controlProperties.Add( SelectedColumn );
controlProperties.Add( CurrentPage.ToString() );

// Create an ObjectStateFormatter to serialize the ArrayList.
ObjectStateFormatter formatter = new ObjectStateFormatter();

// Call the Serialize method to serialize the ArrayList to a Base64 encoded string.
string base64StateString = formatter.Serialize(controlProperties);
Dim controlProperties As New ArrayList(3)

controlProperties.Add(SortDirection)
controlProperties.Add(SelectedColumn)
controlProperties.Add(CurrentPage.ToString())

' Create an ObjectStateFormatter to serialize the ArrayList.
Dim formatter As New ObjectStateFormatter()

' Call the Serialize method to serialize the ArrayList to a Base64 encoded string.
Dim base64StateString As String = formatter.Serialize(controlProperties)

Remarks

Any object graph that is serialized with the Serialize method can be deserialized with the Deserialize method. The Serialize(Object) method is used to serialize an object state graph to a base64-encoded string form.

Applies to

Serialize(Stream, Object)

Serializes an object state graph to the specified Stream object.

public:
 void Serialize(System::IO::Stream ^ outputStream, System::Object ^ stateGraph);
public void Serialize (System.IO.Stream outputStream, object stateGraph);
member this.Serialize : System.IO.Stream * obj -> unit
Public Sub Serialize (outputStream As Stream, stateGraph As Object)

Parameters

outputStream
Stream

A Stream to which the ObjectStateFormatter serializes the state of the specified object.

stateGraph
Object

The object to serialize.

Exceptions

The specified outputStream is null.

Examples

The following code example demonstrates how a class retrieves an ObjectStateFormatter instance to serialize view state and control state to a stream using the Serialize(Stream, Object) method. 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.");
        }
    }
}
'
' 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)

            writer.Write(serializedState)
            writer.Close()
            stateStream.Close()
        Else
            Throw New InvalidOperationException("Session needed for StreamPageStatePersister.")
        End If
    End If
End Sub

Remarks

Any object state graph that is serialized with the Serialize method can be deserialized with the Deserialize method. The Serialize(Stream, Object) method is used to serialize an object state graph to a binary Stream object.

Applies to