Condividi tramite


ObjectStateFormatter.Serialize Metodo

Definizione

Serializza il grafico sullo stato di un oggetto.

Overload

Serialize(Object)

Serializza un grafico sullo stato di oggetti in una stringa con codifica Base64.

Serialize(Stream, Object)

Serializza un grafico sullo stato di un oggetto nell'oggetto Stream specificato.

Serialize(Object)

Serializza un grafico sullo stato di oggetti in una stringa con codifica Base64.

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

Parametri

stateGraph
Object

Oggetto da serializzare.

Restituisce

String

Stringa con codifica Base64 che rappresenta lo stato dell'oggetto serializzato del parametro stateGraph.

Esempio

Nell'esempio di codice seguente viene illustrato come serializzare i valori di un set di proprietà del controllo in una stringa con codifica Base64 usando il Serialize(Object) metodo . La stringa può essere deserializzata in un secondo momento con il Deserialize(String) metodo .


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)

Commenti

Qualsiasi oggetto grafico serializzato con il Serialize metodo può essere deserializzato con il Deserialize metodo . Il Serialize(Object) metodo viene usato per serializzare un grafico dello stato dell'oggetto in un formato stringa con codifica base64.

Si applica a

Serialize(Stream, Object)

Serializza un grafico sullo stato di un oggetto nell'oggetto Stream specificato.

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)

Parametri

outputStream
Stream

Classe Stream in cui la classe ObjectStateFormatter serializza lo stato dell'oggetto specificato.

stateGraph
Object

Oggetto da serializzare.

Eccezioni

Il valore specificato per il parametro outputStream è null.

Esempio

Nell'esempio di codice seguente viene illustrato come una classe recupera un'istanza ObjectStateFormatter di per serializzare lo stato di visualizzazione e il controllo in un flusso usando il Serialize(Stream, Object) metodo . Questo esempio di codice fa parte di un esempio più ampio fornito per la PageStatePersister classe .

//
// 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

Commenti

Qualsiasi grafico dello stato dell'oggetto serializzato con il Serialize metodo può essere deserializzato con il Deserialize metodo . Il Serialize(Stream, Object) metodo viene utilizzato per serializzare un grafico dello stato dell'oggetto in un oggetto binario Stream .

Si applica a