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.

C#
public string Serialize (object stateGraph);

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.

C#

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

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

.NET Framework 4.8.1 ja muud versioonid
Toode Versioonid
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Serialize(Stream, Object)

Serializes an object state graph to the specified Stream object.

C#
public void Serialize (System.IO.Stream outputStream, object stateGraph);

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.

C#
//
// 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.");
        }
    }
}

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

.NET Framework 4.8.1 ja muud versioonid
Toode Versioonid
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1